一个获取当前位置坐标的demo,但是通过navigator.geolocation.getCurrentPosition的回调函数无法获得数据
// 以下为我的js代码  alert后面的注释是出现的错误
$(function(){
          navigator.geolocation.getCurrentPosition(success());
});
function success(p){
          alert(p);  // 输出为 undefined
         // var lng = position.coords.longitude; // 经度
         // var lat = position.coords.latitude; // 纬度 
         // alert(lng+"----"+lat)  // 无法输出
}
请问上面的问题是什么原因?

解决方案 »

  1.   

    Chrome  Firefox 都是这样的错,以开启允许获取地理位置
      

  2.   

    navigator.geolocation.getCurrentPosition(success);
      

  3.   


    这些问题都测试过了,用success   就不能进入success方法...............
      

  4.   

    这个是异步回调函数,你直接就执行来,并且没有传值,那肯定是获取不到东西的啊这个方法,首先是要征求使用者同意之后,才能执行的,而在电脑上,基本上,是不会执行的。按你的方法,这样写一下,是不是就能获取到你要的值了?navigator.geolocation.getCurrentPosition(success(1));如果这么写,是不是你就能得到p=1了,你觉得这样是对的吗?
      

  5.   


    这些问题都测试过了,用success   就不能进入success方法...............你页面允许访问定位服务了吗?
    允许的情况下 才可能调度你的success
      

  6.   


    这些问题都测试过了,用success   就不能进入success方法...............你页面允许访问定位服务了吗?
    允许的情况下 才可能调度你的success这个肯定允许了的
      

  7.   


    直接写 success 就没有任何反应,回调不了success方法。不知道为什么,
    你能写一个能用的demo给我吗?找了很多了,复制网上其他代码也不行.....
      

  8.   

    navigator.geolocation.getCurrentPosition( getPositionSuccess , getPositionError,{
    timeout : 5000 // 5 秒超时
    });
      

  9.   

    navigator.geolocation.getCurrentPosition( getPositionSuccess , getPositionError,{
    timeout : 5000 // 5 秒超时
    });这个方法我试过了... 完全不好使, 在firefox中 点击同意同享地理位置 ,然后就没有然后了
      

  10.   

    http://www.educity.cn/wenda/7540.html 看看吧 应该可以了
      

  11.   

    getCurrentPosition() and watchPosition() are deprecated on insecure origins, and support will be removed in the future. You should consider switching your application to a secure origin, such as HTTPS. See https://goo.gl/rStTGz for more details.
     这是chrome浏览器给我反馈的信息
      

  12.   

    谷歌位置服务被墙了啊,导致无法获取位置信息,开个vps试试,就能取到了