原文连接:
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