zoukankan      html  css  js  c++  java
  • IOS10 window.navigator.geolocation.getCurrentPosition 无法定位问题

    在iOS 10中,苹果对webkit定位权限进行了修改,所有定位请求的页面必须是https协议的。

    如果是非https网页,在http协议下通过HTML5原生定位接口会返回错误,也就是无法正常定位到用户的具体位置,而已经支持https的网站则不会受影响。

    目前提供的解决方案:

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

      2、通过第三方API解决。

    顺便附上定位方法:

        // //定位方法一
    $(function(){
    startgps();
    });
    //获取地理位置方法
    function startgps()
    {
    //判断是否支持
    if (navigator.geolocation)
    {
    navigator.geolocation.getCurrentPosition(showgps,//成功回调函数
    function(error) //失败回调函数
    {
    getPositionError(error);
    },
    {enableHighAcuracy: true, timeout:5000,maximumAge: 0}); // 这里设置超时为5000毫秒,即1秒
    }
    else
    {
    document.getElementById('geo_loc').innerHTML="您的浏览器不支持地图定位";

    }
    }
    function showgps(position)
    {
    document.getElementById('longitude').innerHTML="经度"+position.coords.longitude+ "纬度"+position.coords.latitude;
    getAddress(position.coords.latitude,position.coords.longitude);

    }
    function getPositionError(error){
    switch(error.code){

    case error.TIMEOUT:

    alert("连接超时,请重试");

    break;

    case error.PERMISSION_DENIED:

    alert("您拒绝了使用位置共享服务,查询已取消");

    break;

    case error.POSITION_UNAVAILABLE:

    alert("亲爱的火星网友,非常抱歉,我们暂时无法为您所在的星球提供位置服务");

    break;

    }
    }



    //定位方法二
    if(navigator.geolocation){
    navigator.geolocation.getCurrentPosition(
    function(position){
    document.getElementById('longitude').innerHTML="经度"+position.coords.longitude+ "纬度"+position.coords.latitude;
    document.getElementById('accuracy').innerHTML="误差"+position.coords.accuracy+"米";
    getAddress(position.coords.latitude,position.coords.longitude);
    },
    function(error){
    var errorType = ['您拒绝共享位置信息', '获取不到位置信息', '获取位置信息超时'];
    // alert(errorType[error.code - 1]);
    document.getElementById('geo_loc').innerHTML=error.code+" "+error.message;
    document.getElementById('geo_loc').innerHTML= errorType[error.code - 1];
    }
    );
    }else{
    document.getElementById('geo_loc').innerHTML="您的浏览器不支持地图定位";
    }

    function getAddress(latitude,longitude){
    var geocoder;
    var map = new AMap.Map("container", {
    resizeEnable: true,
    zoom: 18
    })
    var lnglatXY = [longitude,latitude];//需转为地址描述的坐标
    //加载地理编码插件
    var geocoder;
    AMap.service(["AMap.Geocoder"], function() {
    geocoder = new AMap.Geocoder({
    radius: 1000,
    extensions: "all"
    });
    //逆地理编码
    geocoder.getAddress(lnglatXY, function(status, result){
    //取回逆地理编码结果
    if(status === 'complete' && result.info === 'OK'){
    geocoder_CallBack(result);
    }
    });
    });
    }
    function geocoder_CallBack(data) {
    var address = data.regeocode.formattedAddress; //返回地址描述
    document.getElementById("result").innerHTML = address;
    }
  • 相关阅读:
    [原创]在Windows平台使用msvc(cl.exe) + vscode编写和调试C/C++代码
    几种动态调用js函数方案的性能比较
    z-index随笔
    [原]配置多个密钥免密码登录服务器简明教程
    [转]为 windows cmd 设置代理
    [原创]实现多层DIV叠加的js事件穿透
    [转]linux terminal中使用proxy
    [转]jquery中innerWidth(),outerWidth(),outerWidth(true)和width()的区别
    [转]React表单无法输入原因----约束性和非约束性组件
    [原创]aaencode等类似js加密方案破解方法
  • 原文地址:https://www.cnblogs.com/luobiao/p/6237867.html
Copyright © 2011-2022 走看看