uniapp移动端地图,点击气泡弹窗并实现精准定位

记录移动端地图map组件的使用

需求记录:

移动端地图部分需要展示两个定位点,上报点及人员定位点。通过右上角的两个按钮实现地图定位。点击对应定位气泡,弹出定位点的信息。

效果图如下:

map在nvue中的使用。直接用nvue可以直接调整层级,可以覆盖住map组件。在map组件上添加按钮控制,实现交互。map组件内添加按钮使用cover-image如下:

<view>
    <map id="map" class="map" :scale="15" ref="map" style="width:320px;height:500px;margin-left: 30px;"
			:latitude="latitude" :longitude="longitude" :markers="cover">
	    <view class="btnStyle">
		    <cover-image src="../../static/images/people.png" 
			class="removeToReal" @tap="removeToReal"></cover-image>
		    <cover-image src="../../static/images/gaodeMap.png" style="margin-top: 18px;" class="removeToMap" @tap="removeToMap"></cover-image>
	    </view>
	</map>
</view>

js部分:

<script>
    export default {
        data() {
            return {
                // 地图
                mapContext: null,
				latitude: null,
				longitude: null,
                latitudeReal: null,
				longitudeReal: null,
                cover: [],
            }
        },
        methods: {
            //  移动到对应点坐标
            removeToReal() {
				this.mapContext.moveToLocation({
					latitude: this.latitudeReal,
					longitude: this.longitudeReal
				});
			},
            //  移动到对应点坐标
			removeToMap() {
				this.mapContext.moveToLocation({
					latitude: this.latitude,
					longitude: this.longitude
				});
			},
            // 关键代码:
            // 加载坐标点及气泡。(data参数中包含了所需的所有数据,以下仅为气泡弹窗的代码示例参考)
			async setMarkers(data) {
				let latlng = JSON.parse(data.inspectorLocation)
				let reallatlngTurn = this.wgs84_gcj02(latlng[0],latlng[1])
				let realatlng = JSON.parse(data.inspectorRealsite)
				let realTurn = this.wgs84_gcj02(realatlng[0],realatlng[1])
				this.longitude = reallatlngTurn[0]
				this.latitude = reallatlngTurn[1]
				this.longitudeReal = realTurn[0]
				this.latitudeReal = realTurn[1]
				let markers = []
				let markerInspector = {
					id: 'id_' + 'inspectorLocation',
					latitude: reallatlngTurn[1],
					longitude: reallatlngTurn[0],
					width: 30,
					height: 35,
					iconPath: '../../static/images/position2.png',
					callout:{//自定义标记点上方的气泡窗口 点击有效  
					    content: `                     上报点
			任务单号:${data.taskSn}
			任务类型:外部任务
			派发时间:${data.dispatchTime.slice(0,10)}
			上报人:    ${data.inspectorName}
			经度:       ${latlng[0].toFixed(6)}
			纬度:       ${latlng[1].toFixed(6)}`,//文本
					    color: '#ffffff',//文字颜色
					    fontSize: 14,//文本大小
					    borderRadius: 15,//边框圆角
					    borderWidth: '10',
						padding: '10',
						textAlign: 'left',
					    bgColor: '#e51860',//背景颜色
					    display: 'BYCLICK',//常显
					}
				}
				markers.push(markerInspector)
				
				let markerReal = {
					id: 'id_' + 'realLocation',
					latitude: realTurn[1],
					longitude: realTurn[0],
					callout:{//自定义标记点上方的气泡窗口 点击有效
					    content: `                     定位点
			任务单号:${data.taskSn}
			任务类型:外部任务
			上报人:    ${data.inspectorName}
			角色:       ${people}
			派发时间:${data.dispatchTime.slice(0,10)}
			经度:       ${realatlng[0]}
			纬度:       ${realatlng[1]}`,//文本
					    color: '#ffffff',//文字颜色
					    fontSize: 14,//文本大小
					    borderRadius: 15,//边框圆角
					    borderWidth: '10',
						padding: '10',
						textAlign: 'left',
					    bgColor: '#e51860',//背景颜色
					    display: 'BYCLICK',//常显
					}
                }
				markers.push(markerReal)
				this.cover=markers

        },
        onLoad(options) {
			this.mapContext = uni.createMapContext("map", this);
        }
</scritpt>

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
要在 Uniapp 移动端应用中获取定位并在地图上显示当前位置,可以使用以下步骤: 1. 在项目中引入地图 SDK(如百度地图 SDK、高德地图 SDK 等),并按照相应的文档进行配置。 2. 在页面中创建地图组件,并设置地图的初始中心点和缩放级别。 3. 在页面中添加获取定位的按钮,并在按钮的点击事件中调用定位 API 来获取当前位置的经纬度信息。 4. 将获取到的经纬度信息传递给地图 SDK,并使用 SDK 提供的接口在地图上添加当前位置的标记。 以下是一个简单的示例代码: ``` <template> <view> <map :longitude="longitude" :latitude="latitude" :scale="scale" @markertap="onMarkerTap"></map> <button @tap="getLocation">获取位置</button> </view> </template> <script> export default { data() { return { longitude: 0, // 地图中心点的经度 latitude: 0, // 地图中心点的纬度 scale: 16, // 地图的缩放级别 } }, methods: { // 获取定位信息 getLocation() { uni.getLocation({ type: 'gcj02', // 使用国测局坐标系 success: res => { this.longitude = res.longitude this.latitude = res.latitude // 在地图上添加当前位置的标记 uni.addMapMarkers({ id: 0, longitude: res.longitude, latitude: res.latitude, title: '当前位置', iconPath: '当前位置的图标路径', width: 32, height: 32, callout: { content: '当前位置', fontSize: 14, bgColor: '#ffffff', padding: 8, borderRadius: 4, display: 'ALWAYS' } }) }, fail: res => { uni.showToast({ title: '获取位置失败', icon: 'none' }) } }) }, // 点击标记时的事件处理函数 onMarkerTap(e) { uni.showToast({ title: '你点击了当前位置', icon: 'none' }) } } } </script> ``` 需要注意的是,在使用定位 API 时,需要在 `manifest.json` 文件中声明相关权限(如 `location`)。另外,在添加标记时,需要指定标记的图标路径和样式,并可以添加一个气泡提示框来显示标记的信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值