uniapp 写app--实现移动地图坐标始终显示在中间
拖动地图,定位跟随移动,都是用的uni 里的东西,根据经纬度反解析地址还没有解决(有机会再来更新)
使用uniapp里的map 组件
<view >
<map style="width: 1200rpx;height: 300px;" id="map" :latitude="latitude" :longitude="longitude" :markers="markers" enable-poi="true" @tap="updateLocation" @regionchange="regionchange"></map>
</view>
<script>
export default {
data() {
return {
address:'',
id:0, // 使用 marker点击事件 需要填写id
title: 'map',
latitude: 31.849235,//地图中心的经度or纬度
longitude: 117.133214,//地图中心的经度or纬度
markers: [{//定位点1
id:1,
latitude: 31.849235,
longitude: 117.133214,
iconPath: '/static/images/new-location.png'
},
{//定位点2
id: 2,
latitude: 31.849235,
longitude: 117.133214,
iconPath: '/static/images/location.png'
}],
}
},
methods: {
//获取中心点位置
getCenterLatLong(){
let mapCtx =uni.createMapContext("map",this)
let that = this
mapCtx.getCenterLocation({
success:res=> {
// 移动定位点的经纬度位置
that.markers[0].latitude = res.latitude;
that.markers[0].longitude = res.longitude;
that.$forceUpdate(); //真机更改数据后界面不变,强制刷新
},
fail: (res) => {
console.log("失败",res);
}
})
},
regionchange(e){
console.log("regionchange-----------", e);
if (e.type === 'end') { //在安卓中是 end 事件
this.getCenterLatLong() // 地图移动时获取中心点的经纬度
}else if(e.type ==='regionchange'){ // 在ios中是 regionchange
this.getCenterLatLong()
}
},
//获取经纬度
getLocation() {
// 新增获取当前定位
//修改获取数据库定位
uni.getLocation({
type: 'gcj02',
geocode: true, //获取城市具体地址
success: (res) => {
console.log("获取城市具体地址--", res.address)
this.latitude = res.latitude
this.longitude = res.longitude
this.markers[0].latitude = res.latitude
this.markers[0].longitude = res.longitude
this.markers[1].latitude = res.latitude
this.markers[1].longitude = res.longitude
// 改变所属区域
this.bindPickerChange(res.address.district)
// this.openMap()
},
fail: (res) => {
console.log("获取地址失败了", res);
}
})
},
}