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

昨天老闆告訴我代碼有Bug(定位失敗),因而各類測試最終發現IOS10以上版本手機不能成功(窮,買不起iphone,測試不徹底),先貼失敗代碼:javascript

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接口實現,廢話很少,上代碼:html

<!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接口完美解決。java

相關文章
相關標籤/搜索