高德地圖和百度地圖獲取當前位置經緯度

高德
導入高德js
mapObj = new AMap.Map('iCenter');

mapObj.plugin('AMap.Geolocation', function () {
geolocation = new AMap.Geolocation({
enableHighAccuracy: true,//是否使用高精度定位,默認:true
timeout: 10000, //超過10秒後中止定位,默認:無窮大
maximumAge: 0, //定位結果緩存0毫秒,默認:0
convert: true, //自動偏移座標,偏移後的座標爲高德座標,默認:true
showButton: true, //顯示定位按鈕,默認:true
buttonPosition: 'LB', //定位按鈕停靠位置,默認:'LB',左下角
buttonOffset: new AMap.Pixel(10, 20),//定位按鈕與設置的停靠位置的偏移量,默認:Pixel(10, 20)
showMarker: true, //定位成功後在定位到的位置顯示點標記,默認:true
showCircle: true, //定位成功後用圓圈表示定位精度範圍,默認:true
panToLocation: true, //定位成功後將定位到的位置做爲地圖中心點,默認:true
zoomToAccuracy:true //定位成功後調整地圖視野範圍使定位位置及精度範圍視野內可見,默認:false
});
mapObj.addControl(geolocation);
geolocation.getCurrentPosition();
AMap.event.addListener(geolocation, 'complete', onComplete);//返回定位信息
// AMap.event.addListener(geolocation, 'error', onError); //返回定位出錯信息
});緩存

function onComplete(e){
//e中包含詳細信息
map = new AMap.Map('ditu', {

center:[e.position.lng,e.position.lat],
zoom:15
});
this

 
}
function onError(e){
alert('獲取位置出錯')
}
百度
導入百度JS

map = new BMap.Map("ditu"); // 建立地圖實例spa

var geoc = new BMap.Geocoder();
var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(function(r){
  if(this.getStatus() == BMAP_STATUS_SUCCESS){
    var mk = new BMap.Marker(r.point);
var point = new BMap.Point(r.point.lng,r.point.lat); // 建立點座標code

 

}else {
alert("定位失敗")
    console.log('沒法定位到您的當前位置,導航失敗,請手動輸入您的當前位置!'+this.getStatus());
  }
},{enableHighAccuracy: true});get

相關文章
相關標籤/搜索