2016-03-28 58 views
0

我的JS脚本有点问题。 我想确定用户在我的页面上的位置和currant时区。 我使用Firefox,MS Internet Explorer和MS Edge测试了它。问题是,它有时有效,有时不起作用。不知道为什么。与JS的地理位置

在Firefox中我得到了请求,如果我真的允许这个函数。但我没有得到经度和latidute。

如果我尝试一下我的iPhone它一直工作。

function getLocation() { 
     navigator.geolocation.getCurrentPosition(showPosition); 
     function showPosition(position) {; 
      location_latitude = position.coords.latitude 
      location_latitude = location_latitude.toFixed(6); 
      location_longitude = position.coords.longitude 
      location_longitude = location_longitude.toFixed(6) 
      location_accuracy = position.coords.accuracy 
      location_accuracy = location_accuracy.toFixed(2); 

      var gmt = new Date(); 
      var location_gmt_difference = (gmt.getTimezoneOffset()/60/-1).toFixed(1); 

      options_location_input.location_time_zone.value = location_gmt_difference 
      options_location_input.location_latitude.value = location_latitude 
      options_location_input.location_longitude.value = location_longitude 
      options_location_input.location_accuracy.value = location_accuracy 
     }; 
    }; 

有人可以帮助我。我失散了!

回答

0

你是否总是正在服务该页面?如果只是查看文件系统上的页面,它不起作用;该页面必须提供。

+0

嘿,是的。我在我的电脑上使用XAMPP进行服务。 – Kai

+0

如果您在本地运行,您可能会遇到的问题是您的xampp不知道在哪个公共IP地址正在运行,因此无法获取地理定位。无论如何,客户端的地理位置必须由用户(合法)允许,否则您可以在服务器端代码中获取客户端的请求IP地址。 – Kruser

+0

添加一个错误处理函数作为getCurrentPosition的第二个参数来显示为什么它不起作用。 https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition – arkadianriver