最近,在開發二維碼簽到系統時,需要對用戶的簽到的經緯度進行獲取,並且判斷簽到距離是否在可支持范圍內。
使用了高德地圖。因為本人最近剛剛入手了一款新的Iphone7里面是最新的ios11系統。發現我的手機總是定位失敗。后來查閱知,現在Chrome瀏覽器和ios等系統需要HTTPS才可以獲得定位。其他安卓手機都正常。最后在高德地圖的問題中心找到了解決辦法。
蘋果新發的 iOS 11 操作系統的一大特性是對 http 形式訪問頁面的限制變得非常嚴格(相比iOS 10 和 iOS 9)。
高德提供的JS API功能均支持http和https兩種協議訪問,在iOS 11操作系統上推薦使用https形式訪問功能頁面和接口。
- 請先將JS API的版本升級到1.4.1版本:
<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.4.0&key=您申請的key值"></script>
- 然后需要滿足以下兩點:
1、需要使用JS API的高精度定位功能,在iOS 11上,請通過https訪問定位。
2、您的個人(或企業)網站在iOS 11系統下也請您調整成https形式對終端用戶提供。
如果修改之后依舊發生定位失敗的情況,請參考如下:
- 類似 Chrome 等瀏覽器框架目前禁止非 https 環境進行定位,請在iOS11下嘗試訪問如下頁面:
1、可以成功定位的頁面地址:http://a.amap.com/jsapi_demos/static/remogeo/remo.html
2、需要引入的remogeo.js ,建議放置在本地:http://a.amap.com/jsapi_demos/static/remogeo/remogeo.js
3、重點需要關注的代碼:
<!-- 加載遠程定位腳本 -->
<script type="text/javascript" src="./remogeo.js"></script>
// ios環境切換到使用遠程https定位 if (AMap.UA.ios && document.location.protocol !== 'https:') { //使用遠程定位,見 remogeo.js var remoGeo = new RemoGeoLocation(); //替換方法 navigator.geolocation.getCurrentPosition = function() { return remoGeo.getCurrentPosition.apply(remoGeo, arguments); }; //替換方法 navigator.geolocation.watchPosition = function() { return remoGeo.watchPosition.apply(remoGeo, arguments); }; }
- iOS 11下WebView定位超時可做如下調整:
//在info.plist添加 Privacy - Location When In Use Usage Description
日志現象:
webview[6877:601452] This app has attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSLocationWhenInUseUsageDescription key with a string value explaining to the user how the app uses this data