問題描述
使用plus.geolocation.getCurrentPosition()這個方法獲取定位信息撕捍,在iPhone上沒有任何問題忧风,在安卓上真機調試也沒有問題呕诉,但是云端打包后再安卓上獲取不到address信息贴硫。
原創(chuàng)文章,歡迎轉載.轉載請注明出處: http://www.reibang.com/p/d9ee48dd323f
實現(xiàn)代碼如下:
//獲取位置信息
plus.geolocation.getCurrentPosition(function(p){
mui.toast(JSON.stringify(p));
if (!p || !p.address) {
return;
}
//經緯度
console.log('Geolocation\nLatitude:' + p.coords.latitude + '\nLongitude:' + p.coords.longitude + '\nAltitude:' + p.coords.altitude);
//JSON對象亦渗,地址信息
console.log(JSON.stringify(p.address));
}, function(e){
console.log('Geolocation error: ' + e.message);
if (isShowToast) {
mui.toast("定位出錯:"+e.message);
}
});
mui.toast(JSON.stringify(p))展示的信息如下:
沒有address
解決方法
云端打包后默認按以下優(yōu)先順序使用定位(高德定位>百度定位>系統(tǒng)定位)菱涤,由于沒有集成高德和百度定位,所以使用的是系統(tǒng)定位殷勘。集成了百度定位就可以了:
依然使用上面的方法,只不過給plus.geolocation.getCurrentPosition()多加一個參數(shù){provider:'baidu'}昔搂,具體代碼如下:
//使用百度地圖地位模塊獲取位置信息
plus.geolocation.getCurrentPosition(function(p){
mui.toast(JSON.stringify(p));
if (!p || !p.address) {
return;
}
//經緯度
console.log('Geolocation\nLatitude:' + p.coords.latitude + '\nLongitude:' + p.coords.longitude + '\nAltitude:' + p.coords.altitude);
//JSON對象玲销,地址信息
console.log(JSON.stringify(p.address));
}, function(e){
console.log('Geolocation error: ' + e.message);
if (isShowToast) {
mui.toast("定位出錯:"+e.message);
}
}, {provider:'baidu'});
然后點擊manifest.json文件,選SDK配置摘符,配好對應appkey就好了贤斜,appkey的話去百度地圖開放平臺申請。
注意:申請Android端appkey時需要填寫【發(fā)布版SHA1】逛裤,如果使用Hbuilder官方提供的Google賬號打包瘩绒,這里就默認填寫官方提供的SHA1:BA:AD:09:3A:82:82:9F:B4:32:A7:B2:8C:B4:CC:F0:E9:F3:7D:AE:58