1. 程式人生 > >高德地圖和百度地圖獲取當前位置經緯度

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

高德 匯入高德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
});

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

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

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); // 建立點座標

 

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