用navigator.geolocation在IOS10以上的系統無法定位

來源:互聯網
上載者:User

標籤:也有   無法   介面   play   定位   根據   禁止使用者   申請   etl   

昨天老闆告訴我代碼有Bug(定位失敗),於是各種測試最終發現IOS10以上版本手機不能成功,先貼失敗代碼:

var city ="";                navigator.geolocation.getCurrentPosition(function(position) {                    var lat = position.coords.latitude;                    var lon = position.coords.longitude;                    var point = new BMap.Point(lon, lat); // 建立座標點                    // 根據座標得到地址描述                    var myGeo = new BMap.Geocoder();                                        myGeo.getLocation(point, function(result) {                        city= result.addressComponents.city;                        alert(city);                    });                });

經過查閱資料得知IOS系統在10以上版本考慮到安全問題禁止使用者在http協議下定位,要想定位必須將http協議升級成https協議,當然也有別的辦法,比如調用百度地圖API介面實現,廢話不多,上代碼:

<!DOCTYPE html><html><head>    <meta charset="UTF-8">    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=你的Key(在百度地圖免費申請)"></script>    <title>定位</title></head><body>    <div id="allmap" style="width: 0;height: 0; display: none;"></div>    <script type="text/javascript">    // 百度地圖API功能    var map = new BMap.Map("allmap");    var point = new BMap.Point(108.95,34.27);    map.centerAndZoom(point,12);    var geolocation = new BMap.Geolocation();    geolocation.getCurrentPosition(function(r){console.log(r.point)        if(this.getStatus() == BMAP_STATUS_SUCCESS){            var mk = new BMap.Marker(r.point);            map.addOverlay(mk);            map.panTo(r.point);            var point = new BMap.Point(r.point.lng,r.point.lat);            var gc = new BMap.Geocoder();            gc.getLocation(point, function(rs){               var addComp = rs.addressComponents;                console.log(rs);                   alert(addComp.city+"==="+addComp.district)            });        }else {            alert(‘failed‘+this.getStatus());        }    },{enableHighAccuracy: true})    </script>  </body>

利用百度地圖API介面完美解決。

用navigator.geolocation在IOS10以上的系統無法定位

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.