为什么地理定位不使用手机GPS

时间:2012-11-24 09:13:26

标签: javascript geolocation

我使用以下内容来获取手机的位置..

  <script src="http://maps.google.com/maps/api/js?sensor=true"></script>

  <script type="text/javascript" >
   $(document).ready(function () {

 // wire up button click
 $('#go').click(function () {
    // test for presence of geolocation
     if (navigator && navigator.geolocation) {
         // make the request for the user's position
         navigator.geolocation.getCurrentPosition(geo_success, geo_error);
     } 
 });
 });


 function geo_success(position) {
  printAddress(position.coords.latitude, position.coords.longitude);
  alert(position.coords.latitude + " " + position.coords.longitude)
 }


  // use Google Maps API to reverse geocode our location
   function printAddress(latitude, longitude) {
 // set up the Geocoder object
  var geocoder = new google.maps.Geocoder();

 // turn coordinates into an object
 var yourLocation = new google.maps.LatLng(latitude, longitude);


 // find out info about our location
 geocoder.geocode({ 'latLng': yourLocation }, function (results, status) {
     if (status == google.maps.GeocoderStatus.OK) {
         if (results[0]) {
             $('body').append('<p>Your Address:<br />' +
                 results[0].formatted_address + '</p>');
         } else {
             error('Google did not return any results.');
         }
     } else {
         error("Reverse Geocoding failed due to: " + status);
     }
 });

 }
 }

结果永远不会来自手机GPS。它确实要求获得跟踪位置的许可。它确实返回当地周围的随机位置。

为什么会这样?

3 个答案:

答案 0 :(得分:1)

简单的答案,没有答案,所有手机都以不同的方式对待。我发现强制gps的唯一方法是使用原生应用

答案 1 :(得分:0)

这取决于设备。你会发现很多设备确实会返回正确的位置。

您可能会注意到,当GPS初始化时,它没有很好的卫星定位,并且为您提供最后已知的位置,基于网络数据的位置或基于有限卫星的位置数据

也可以将某些设备配置为仅共享“粗略”位置,这不是特定的或尽可能准确的。

答案 2 :(得分:0)

大多数智能手机都有多种自我定位方式,例如: GPS用于高精度(精细)定位和蜂窝塔位置,用于低精度(粗略)定位。在您的成功函数中返回的位置,您应该在coords.accuracy中估计准确度。

进行位置查询时,您可以通过指定enableHighAccuracy = true

来请求高精度响应
 navigator.geolocation.getCurrentPosition(geo_success, geo_error,
       {enableHighAccuracy = true});

请注意,这可能并不总是有效,因为有些设备不允许您访问高精度定位(出于多种考虑,其中一个因素是GPS定位会对电池使用产生影响)。