酸狗先带大家看看效果: 地图咋实现的就不写了小程序的map的API写的很清楚,主要看下转百度经纬度,想是uniapp搭建的,为啥用uniapp,以为不想用小程序开发工具~
获取下经纬度然后把经纬度存起来:
getLocation() {
const that = this
uni.getLocation({
type: 'gcj02',
altitude: true,
success(res) {
console.log(res)
that.latitude = res.latitude;
that.longitude = res.longitude;
that.nearby_search();
if (that.wechat == 1) {
that.formatDegree(that.latitude);
that.latitude = that.formatDegree(that.latitude);
that.formatDegree(that.longitude);
that.longitude = that.formatDegree(that.longitude);
}
var lng = res.longitude;
var lat = res.latitude;
that.gcj2bdString(lng, lat);
that.baidulongitude = (that.gcj2bdString(lng, lat).lng).toFixed(5);
that.baidulatitude = (that.gcj2bdString(lng, lat).lat).toFixed(5);
if (that.wechat == 1) {
that.formatDegree(that.baidulongitude);
that.baidulongitude = that.formatDegree(that.baidulongitude);
that.formatDegree(that.baidulatitude);
that.baidulatitude = that.formatDegree(that.baidulatitude);
}
}
})
},
火星坐标系 (GCJ-02) 转百度坐标系 (BD-09):
因为除了百度地图,不管是高德地图、腾讯地图、谷歌地图用的都是火星坐标系 (GCJ-02),所以就需要百度转换下,我也是服了!
gcj2bdString(lng, lat) {
const xpi = 3.14159265358979324 * 3000.0 / 180.0
const z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * xpi);
const theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * xpi);
return {
lng: z * Math.cos(theta) + 0.0065,
lat: z * Math.sin(theta) + 0.006
}
},
最后是经纬度转度分秒:
formatDegree(value) {
if (value != null && value != '') {
value = Math.abs(value);
var v1 = Math.floor(value);
var v2 = Math.floor((value - v1) * 60);
var v3 = Math.round((value - v1) * 3600 % 60);
return v1 + "°" + v2 + "′" + v3 + "″";
} else {
return '' + "°" + '' + "′" + '' + "″";
}
},
希望对各位童鞋有帮助哈~
|