• 完美解决window.navigator.geolocation.getCurrentPosition,在IOS10系统中无法定位问题


    目前由于许多用户都将电话升级到了IOS系统,苹果的iOS 10已经正式对外推送,相信很多用户已经更新到了最新的系统。然而,如果web站没有及时支持https协议的话,当很多用户在iOS 10下访问很多网站时,会发现都无法进行正常精确定位,导致部分网站的周边推荐服务无法正常使用。为何在iOS 10下无法获取当前位置信息?这是因为在iOS 10中,苹果对webkit定位权限进行了修改,所有定位请求的页面必须是https协议的。如果是非https网页,在http协议下通过html5原生定位接口会返回错误,也就是无法正常定位到用户的具体位置,而已经支持https的网站则不会受影响。

    目前提供的解决方案:

    1、将网站的http设置为Https。

    2、通过第三方解决,这也是我目前使用的方法。

    首先看下代码差异:

    1、在页面引入js

    <script src="/Content/Scripts/jquery.flexslider.js"></script>  
    <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>  

    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);     });
    }

    网站不支持https访问

    1、页面引入js

    <script src="/Content/Scripts/jquery.flexslider.js"></script>  
    <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> 
    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;  
            //获取地理位置成功,跳转  
    
          }); 
      }
    }) 

    目前获取定位的方法都在这里,仅供大家参考使用!

  • 相关阅读:
    (转)python字符串函数
    分享二:架构设计分享一:关于API分布式服务提供方式
    架构设计分享一:关于分布式系统的数据一致性问题(一)
    分享四:分布式事务设计-两段式提交
    分享三:mysql跨库查询
    PHP笔试题
    我的mysql学习心得
    linux分享一:进程全攻略--守护进程(服务)
    分享二:签名原理与算法
    linux命令详解:pgrep命令
  • 原文地址:https://www.cnblogs.com/henuyuxiang/p/7525314.html
Copyright © 2020-2023  润新知