ios10不能定位 window.navigator.geolocation.getCurrentPosition(定位第一节)

原文连接:

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

 
  1. <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=6yAoynmTPNlTBa8z1X4LfwGE"></script>

  2. <script type="text/javascript" src="http://developer.baidu.com/map/jsdemo/demo/convertor.js"></script>

         2、获得定位方法  window.navigator.geolocation.getCurrentPosition:通过手机的webKit定位(目前ios系统对非https网站不提供支持)

 
  1. navigator.geolocation.getCurrentPosition(translatePoint); //定位

  2. function translatePoint(position) {

  3. var currentLat = position.coords.latitude;

  4. var currentLon = position.coords.longitude;

  5. SetCookie("curLat", currentLat, 1);//设置cookie

  6. SetCookie("curLng", currentLon, 1);//设置cookie

  7. var gpsPoint = new BMap.Point(currentLon, currentLat);

  8.  
  9. var pt = new BMap.Point(currentLon, currentLat);

  10. var geoc = new BMap.Geocoder();

  11. geoc.getLocation(pt, function (rs) {

  12. var addComp = rs.addressComponents;

  13. SetCookie("curLat", currentLat, 1); //设置cookie

  14. SetCookie("curLng", currentLon, 1); //设置cookie

  15. //alert(JSON.stringify(addComp));

  16. var city = addComp.city;

  17. //获得具体街道信息

  18. var texts = addComp.district + "-" + addComp.street + "-" + addComp.streetNumber;

  19. $("#nowRoad").text(texts);

  20. });

  21. }


 

代码段二:

          1、在页面引入js

 
  1. <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=6yAoynmTPNlTBa8z1X4LfwGE"></script>

  2. <script type="text/javascript" src="http://developer.baidu.com/map/jsdemo/demo/convertor.js"></script>

           2、获得定位方法

 
  1. var geolocation = new BMap.Geolocation();

  2. geolocation.getCurrentPosition(function (r) {

  3. if (this.getStatus() == BMAP_STATUS_SUCCESS) {

  4. var mk = new BMap.Marker(r.point);

  5. currentLat = r.point.lat;

  6. currentLon = r.point.lng;

  7. SetCookie("curLat", currentLat, 1); //设置cookie

  8. SetCookie("curLng", currentLon, 1); //设置cookie

  9. var pt = new BMap.Point(currentLon, currentLat);

  10. var geoc = new BMap.Geocoder();

  11. geoc.getLocation(pt, function (rs) {

  12. var addComp = rs.addressComponents;

  13. SetCookie("curLat", currentLat, 1); //设置cookie

  14. SetCookie("curLng", currentLon, 1); //设置cookie

  15.  
  16. var city = addComp.city;

  17. var addComp = rs.addressComponents;

  18. var texts = addComp.district + "-" + addComp.street + "-" + addComp.streetNumber;

  19. //获取地理位置成功,跳转

  20.  
  21. });

  22. }

  23. });

以上两端代码中,最大的不同点在于:

第一段代码使用的是

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

猜你喜欢

转载自blog.csdn.net/zengmingen/article/details/82593351