js百度地图android定位不准,百度地图js定位不准
瀏覽器H5定位即navigator.geolocation,通過(guò)IP或者域名的形式,如:http://172.21.3.82:8080 和http://b.cunzhang.com進(jìn)行訪問(wèn)時(shí),調(diào)用navigator.geolocation.getCurrentPosition,打開(kāi)控制臺(tái),你會(huì)發(fā)現(xiàn)有以下錯(cuò)誤信息:
Only secure origins are allowed (see: https://goo.gl/Y0ZkNV).
“只有在安全來(lái)源的情況才才被允許”。錯(cuò)誤信息里還包含了一個(gè)提示鏈接,我們不妨打開(kāi)這個(gè)
鏈接https://goo.gl/Y0ZkNV)看看。原來(lái),為了保障用戶的安全,Chrome瀏覽器認(rèn)為只有安全的
來(lái)源才能開(kāi)啟定位服務(wù)。那什么樣才算是安全的來(lái)源呢?在打開(kāi)鏈接的頁(yè)面上有這么一段話:
“Secure origins” are origins that match at least one of the following (scheme, host, port) patterns:(https, *, *)
(wss, *, *)
(*, localhost, *)
(*, 127/8, *)
(*, ::1/128, *)
(file, *, —)
(chrome-extension, *, —)
This list may be incomplete, and may need to be changed. Please discuss!
大概意思是說(shuō)只有包含上述列表中的scheme、host或者port才會(huì)被認(rèn)為是安全的來(lái)源,現(xiàn)在這個(gè)列表還不夠完整,后續(xù)可能還會(huì)有變動(dòng),有待討論。
如果需要在域名訪問(wèn)的基礎(chǔ)上實(shí)現(xiàn)地位位置的定位,那我們只能把http協(xié)議升級(jí)為https了。
html>
body,?html,?#allmap?{
width:?100%;
height:?100%;
overflow:?hidden;
margin:?0;
font-family:?"微軟雅黑";
}
地圖展示//?百度地圖API功能
var?map?=?new?BMap.Map("allmap");????//?創(chuàng)建Map實(shí)例
var?point?=?new?BMap.Point(116.331398,?39.897445);
map.centerAndZoom(point,?30);
map.enableScrollWheelZoom(true);?????//開(kāi)啟鼠標(biāo)滾輪縮放
//添加地圖類型控件
map.setCurrentCity("北京");
var?geolocation?=?new?BMap.Geolocation();
geolocation.getCurrentPosition(function?(r)?{
if?(this.getStatus()?==?BMAP_STATUS_SUCCESS)?{
var?mk?=?new?BMap.Marker(r.point);
map.addOverlay(mk);
map.panTo(r.point);
point?=?r.point;
console.log(r.point.lng?+?','?+?r.point.lat);
alert('您的位置:'?+?r.point.lng?+?','?+?r.point.lat);
getName(point);
}
else?{
alert('failed'?+?this.getStatus());
}
},?{?enableHighAccuracy:?true?})
function?callback(data)?{
console.log(data);
console.log('轉(zhuǎn)化后:'+data.points[0].lng?+?','?+?data.points[0].lat);
alert('您的真實(shí)位置:'?+?data.points[0].lng?+?','?+?data.points[0].lat);
var?newpoint?=?new?BMap.Point(data.points[0].lng,?data.points[0].lat);
var?mk?=?new?BMap.Marker(newpoint);
map.addOverlay(mk);
map.panTo(newpoint);
getName(newpoint);
}
function?getName(point)?{
var?geoc?=?new?BMap.Geocoder();
geoc.getLocation(point,?function?(rs)?{
var?addComp?=?rs.addressComponents;
console.log(addComp);
alert(addComp.province?+?",?"?+?addComp.city?+?",?"?+
addComp.district?+?",?"?+?addComp.street?+?",?"?+?addComp.streetNumber);
});
}
//瀏覽器H5定位
if?(navigator.geolocation)?{
navigator.geolocation.getCurrentPosition(function?(position)?{
//得到html5定位結(jié)果
var?x?=?position.coords.longitude;
var?y?=?position.coords.latitude;
console.log(position.coords);
alert('您的位置:'?+?x?+?','?+?y);
//由于html5定位的結(jié)果是國(guó)際標(biāo)準(zhǔn)gps,所以需要轉(zhuǎn)為百度坐標(biāo)系,from=1,
to=5
var?newpoint?=?new?BMap.Point(x,?y);
var?convertor?=?new?BMap.Convertor();
var?pointArr?=?[];
pointArr.push(newpoint);
convertor.translate(pointArr,?1,?5,?callback)
},?function?(e)?{
console.log(e);
alert(e.message);
})
}?else?{
alert("沒(méi)有定位權(quán)限");
}
在https站點(diǎn)下,通過(guò)對(duì)比我們發(fā)現(xiàn)百度api獲取的坐標(biāo)和瀏覽器H5經(jīng)過(guò)坐標(biāo)轉(zhuǎn)換獲得的坐標(biāo)是一致的。所以要想在瀏覽器中通過(guò)百度api獲取精確的定位,最好把站點(diǎn)升級(jí)為https協(xié)議。
總結(jié)
以上是生活随笔為你收集整理的js百度地图android定位不准,百度地图js定位不准的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python excel 数据匹配实现v
- 下一篇: java 数据队列_Java 数据结构