原文連接:
https://blog.csdn.net/michael_ouyang/article/details/54137709
---------------------------------------------------------?
問題分析:
目前由于許多蘋果用戶都升級到了iOS系統,蘋果的iOS 10已經正式對外推送,相信很多用戶已經更新到了最新的系統。然而,如果web站沒有及時支持https協議的話,當很多用戶在iOS 10下訪問很多網站時,會發現都無法進行正常精確定位,導致部分網站的周邊推薦服務無法正常使用。為何在iOS 10下無法獲取當前位置信息?這是因為在iOS 10中,蘋果對webkit定位權限進行了修改,所有定位請求的頁面必須是https協議的。如果是非https網頁,在http協議下通過html5原生定位接口會返回錯誤,也就是無法正常定位到用戶的具體位置,而已經支持https的網站則不會受影響。
?
? ?目前提供的解決方案:
? 1、將網站的http設置為Https。
? 2、通過第三方解決,這也是我目前使用的方法。
?
?
? ?首先看以下兩段代碼:
代碼段一:
? ? ? ? ??1、在頁面引入js
?
-
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=6yAoynmTPNlTBa8z1X4LfwGE"></script>
-
<script type="text/javascript" src="http://developer.baidu.com/map/jsdemo/demo/convertor.js"></script>
?
?
? ? ? ? ?2、獲得定位方法??window.navigator.geolocation.getCurrentPosition:通過手機的webKit定位(目前ios系統對非https網站不提供支持)
?
-
navigator.geolocation.getCurrentPosition(translatePoint); //定位
-
function translatePoint(position) {
-
var currentLat = position.coords.latitude;
-
var currentLon = position.coords.longitude;
-
SetCookie("curLat", currentLat, 1);//設置cookie
-
SetCookie("curLng", currentLon, 1);//設置cookie
-
var gpsPoint = new BMap.Point(currentLon, currentLat);
- ?
-
var pt = new BMap.Point(currentLon, currentLat);
-
var geoc = new BMap.Geocoder();
-
geoc.getLocation(pt, function (rs) {
-
var addComp = rs.addressComponents;
-
SetCookie("curLat", currentLat, 1); //設置cookie
-
SetCookie("curLng", currentLon, 1); //設置cookie
-
//alert(JSON.stringify(addComp));
-
var city = addComp.city;
-
//獲得具體街道信息
-
var texts = addComp.district + "-" + addComp.street + "-" + addComp.streetNumber;
-
$("#nowRoad").text(texts);
-
});
-
}
?
?
代碼段二:
? ? ? ? ??1、在頁面引入js
?
-
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=6yAoynmTPNlTBa8z1X4LfwGE"></script>
-
<script type="text/javascript" src="http://developer.baidu.com/map/jsdemo/demo/convertor.js"></script>
?
?
? ? ? ? ? ?2、獲得定位方法
?
-
var geolocation = new BMap.Geolocation();
-
geolocation.getCurrentPosition(function (r) {
-
if (this.getStatus() == BMAP_STATUS_SUCCESS) {
-
var mk = new BMap.Marker(r.point);
-
currentLat = r.point.lat;
-
currentLon = r.point.lng;
-
SetCookie("curLat", currentLat, 1); //設置cookie
-
SetCookie("curLng", currentLon, 1); //設置cookie
-
var pt = new BMap.Point(currentLon, currentLat);
-
var geoc = new BMap.Geocoder();
-
geoc.getLocation(pt, function (rs) {
-
var addComp = rs.addressComponents;
-
SetCookie("curLat", currentLat, 1); //設置cookie
-
SetCookie("curLng", currentLon, 1); //設置cookie
- ?
-
var city = addComp.city;
-
var addComp = rs.addressComponents;
-
var texts = addComp.district + "-" + addComp.street + "-" + addComp.streetNumber;
-
//獲取地理位置成功,跳轉
- ?
-
});
-
}
-
});
?
以上兩端代碼中,最大的不同點在于:
第一段代碼使用的是
window.navigator.geolocation.getCurrentPosition()
此方法是通過UIwebview的內核webKit進行定位
?
第二段代碼使用的是
var geolocation = new BMap.Geolocation(); ?
geolocation.getCurrentPosition()
此方式是使用了百度api
附相關連接:http://lbsyun.baidu.com/cms/jsapi/reference/jsapi_reference.html
?
?
另外,值得注意的一個問題是:
使用window.navigator.geolocation.getCurrentPosition()?獲取到的一個經緯度,還不能直接使用,還需要經過解密,才能獲取到準備的地理位置
想知道如何把這個位置進行解密轉換,請留意下一遍文章
http://blog.csdn.net/michael_ouyang/article/details/54378338
?
原文鏈接:http://blog.csdn.net/for12/article/details/52803787