利用HBuilder開發基於MUI的H5+ app中使用百度地圖定位功能


定位功能有兩種方法:

首先要初始化內置地圖:

var map = new plus.maps.Map("map");

這里黃色的map是html里面的id:

<div id="map">地圖加載中...</div>

1、調用GPS定位API(注意,使用了baidubd09ll參數,無需轉換坐標)

 1 plus.geolocation.getCurrentPosition(function(p) {
 2         console.log(p.addresses);
 3         console.log( p.coords.longitude);
 4         console.log( p.coords.latitude);
 5         var gpsPoint = new plus.maps.Point( p.coords.longitude,  p.coords.latitude);
 6         map.centerAndZoom(gpsPoint, 19);
 7         
 8     }, function(e) {}, {
 9         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));
        });
    }
});

 

掃一掃關注,一起學編程:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM