這是一篇關(guān)于WGS-84坐標(biāo)系,GCJ-02坐標(biāo)系瘾蛋,BD09坐標(biāo)系相互轉(zhuǎn)換的文章俐镐,方法都是網(wǎng)上查詢得到的,并非原創(chuàng)哺哼。在我的項(xiàng)目中使用到地圖定位功能京革,后臺(tái)中返回兩種坐標(biāo)系經(jīng)緯度,一種是百度地圖獲取的經(jīng)緯度幸斥,另一種是GPS獲取的經(jīng)緯度匹摇。
首先介紹一下當(dāng)前的互聯(lián)網(wǎng)的坐標(biāo)系都有哪些坐標(biāo)系
地圖坐標(biāo)(WGS84)
-國(guó)際標(biāo)準(zhǔn),從專業(yè)GPS 設(shè)備中取出的數(shù)據(jù)的坐標(biāo)系
-國(guó)際地圖提供商使用的坐標(biāo)系
火星坐標(biāo) (GCJ-02)也叫國(guó)測(cè)局坐標(biāo)系
-中國(guó)標(biāo)準(zhǔn)甲葬,從國(guó)行移動(dòng)設(shè)備中定位獲取的坐標(biāo)數(shù)據(jù)使用這個(gè)坐標(biāo)系
-國(guó)家規(guī)定: 國(guó)內(nèi)出版的各種地圖系統(tǒng)(包括電子形式)廊勃,必須至少采用GCJ-02對(duì)地理位置進(jìn)行首次加密。
百度坐標(biāo) (BD-09)
-百度標(biāo)準(zhǔn),百度 SDK坡垫,百度地圖梭灿,Geocoding 使用
需要注意的是小程序默認(rèn)的是WGS84
第一種方法百度經(jīng)緯度轉(zhuǎn)換騰訊經(jīng)緯度
微信小程序-百度地圖坐標(biāo)轉(zhuǎn)騰訊地圖坐標(biāo)
//百度經(jīng)緯度轉(zhuǎn)換騰訊經(jīng)緯度
reverseLocation: function() {
var that = this;
// 實(shí)例化API核心類
var demo = new QQMapWX({
key: '你的秘鑰'
});
// 調(diào)用接口
demo.reverseGeocoder({
location: {
latitude: that.data.latitude,
longitude: that.data.longitude
},
coord_type: 3, //baidu經(jīng)緯度
success: function(res) {
var latitude = res.result.ad_info.location.lat;
var longitude = res.result.ad_info.location.lng;
var markers = [{
iconPath: '../../../images/common/location.png',
longitude: longitude,
latitude: latitude,
id: "map",
}]
console.log(markers)
that.setData({
markers: markers,
longitude: longitude,
latitude: latitude
})
}
});
},
第二種方法
這里面有詳細(xì)介紹
markerMap: function() {
var latitude = this.data.latitude
var longitude = this.data.longitude
var resultMap = gcoord.transform(
[latitude, longitude], // 經(jīng)緯度坐標(biāo)
gcoord.WGS84, // 當(dāng)前坐標(biāo)系
gcoord.GCJ02
)
var latitudeMap = resultMap[0]
var longitudeMap = resultMap[1]
var markers = [{
iconPath: '../../../images/common/location.png',
latitude: latitudeMap,
longitude: longitudeMap,
id: "map",
}]
console.log(markers)
this.setData({
markers: markers,
latitude: latitudeMap,
longitude: longitudeMap
})
},
第三個(gè),也是解決我問(wèn)題的方法(推薦)
原文鏈接
百度坐標(biāo)(BD09)冰悠、國(guó)測(cè)局坐標(biāo)(火星坐標(biāo)堡妒,GCJ02)、和WGS84坐標(biāo)系互轉(zhuǎn)
小程序安裝方法
npm i coordtransform -S --production
//國(guó)測(cè)局坐標(biāo)(火星坐標(biāo),比如高德地圖在用),百度坐標(biāo),wgs84坐標(biāo)(谷歌國(guó)外以及絕大部分國(guó)外在線地圖使用的坐標(biāo))
var coordtransform=require('coordtransform');
//百度經(jīng)緯度坐標(biāo)轉(zhuǎn)國(guó)測(cè)局坐標(biāo)
var bd09togcj02=coordtransform.bd09togcj02(116.404, 39.915);
//國(guó)測(cè)局坐標(biāo)轉(zhuǎn)百度經(jīng)緯度坐標(biāo)
var gcj02tobd09=coordtransform.gcj02tobd09(116.404, 39.915);
//wgs84轉(zhuǎn)國(guó)測(cè)局坐標(biāo)
var wgs84togcj02=coordtransform.wgs84togcj02(116.404, 39.915);
//國(guó)測(cè)局坐標(biāo)轉(zhuǎn)wgs84坐標(biāo)
var gcj02towgs84=coordtransform.gcj02towgs84(116.404, 39.915);
console.log(bd09togcj02);
console.log(gcj02tobd09);
console.log(wgs84togcj02);
console.log(gcj02towgs84);
//result
//bd09togcj02: [ 116.39762729119315, 39.90865673957631 ]
//gcj02tobd09: [ 116.41036949371029, 39.92133699351021 ]
//wgs84togcj02: [ 116.41024449916938, 39.91640428150164 ]
//gcj02towgs84: [ 116.39775550083061, 39.91359571849836 ]
marker:function(){
var wgs84togcj02 = markerJS.wgs84togcj02(this.data.longitude, this.data.latitude);
var gpslatitude = wgs84togcj02[1];
var gpslongitude = wgs84togcj02[0];
var markers = [{
iconPath: '../../../images/common/location.png',
latitude: gpslatitude,
longitude: gpslongitude,
id: "map",
}]
console.log(markers)
this.setData({
markers: markers,
latitude: gpslatitude,
longitude: gpslongitude
})
},
希望能幫到你溉卓!