百度API獲取地理位置

一、問題1:百度API經過chorme瀏覽器獲取的位置座標偏差有點大。使用IE瀏覽器能準肯定位。瀏覽器

二、說法1:網上有人說chorme瀏覽器如今只支持https協議的網站函數

因爲百度API是http協議,因此百度API加載出了問題網站

三、說話2:chorme瀏覽器的座標和百度的座標不同,須要轉換。將百度API裏的座標轉換實例代碼粘貼下來,裏面的new BMap.Convertor().translate(pointArr, 3, 5, translateCallback)方法裏的回調函數不執行,但在百度API的demo裏卻執行。spa

四、問題2:忽然出現你申請的百度密鑰不正確...,但過幾天卻沒有問題。code

五、chorme不許確暫時沒法解決。orm

 

添加百度API:blog

js代碼以下:get

 var ggPoint = null;
    var bm = null;
    var geolocation = new BMap.Geolocation();
    geolocation.getCurrentPosition(function (r) {
        if (geolocation.getStatus()==BMAP_STATUS_SUCCESS) {
            bm = new BMap.Map("allmap");
            ggPoint = r.point;
            var mk = new BMap.Marker(r.point);
            bm.addOverlay(mk);
            bm.centerAndZoom(ggPoint, 12);
            bm.addControl(new BMap.NavigationControl());

            var convertor = new BMap.Convertor();
            var pointArr = [];
            pointArr.push(ggPoint);
            convertor.translate(pointArr, 1, 5, translateCallback);

            //座標轉換完以後的回調函數
            translateCallback = function (data) {
                if (data.status === 0) {
                    var marker = new BMap.Marker(data.points[0]);
                    bm.addOverlay(marker);
                    var label = new BMap.Label("轉換後的百度標註(正確)", { offset: new BMap.Size(20, -10) });
                    marker.setLabel(label); //添加百度label
                    bm.setCenter(data.points[0]);
                }
            }
                                 
        }
    });
相關文章
相關標籤/搜索