定位功能有兩種方法:
首先要初始化內置地圖:
var map =new BMap.Map("map");
在html中需要定義好id
<div id="map"></div>
1、調用GPS定位API(注意,使用了baidu和bd09ll參數,無需轉換坐標)
mui.plusReady(function() {//表示頁面加載事件 plus.geolocation.getCurrentPosition(function(p) { console.log(p.addresses); console.log( p.coords.longitude); console.log( p.coords.latitude); var gpsPoint = new BMap.Point( p.coords.longitude, p.coords.latitude); map.centerAndZoom(gpsPoint, 19); }, function(e) {}, { provider: 'baidu', coordsType: 'bd09ll' }); })
2、使用h5+內置地圖進行定位
map.getUserLocation(function(state, pos) { console.log(JSON.stringify(pos)); if(0 == state) { map.centerAndZoom(pos, 16); plus.maps.Map.reverseGeocode(pos, {}, function(event) { var address = event.address; // 轉換后的地理位置 var point = event.coord; // 轉換后的坐標信息 var coordType = event.coordType; // 轉換后的坐標系類型 alert("Address:" + address); console.log(JSON.stringify(point)); console.log(coordType); }, function(e) { alert("Failed:" + JSON.stringify(e)); }); } });