2015-06-12 25 views
2

如何提高此代碼中的GPS精度?提高GPS精度(在HTML5地理位置)

var map; 

function initialize() { 
    var mapOptions = { 
    zoom: 18 
    }; 
    map = new google.maps.Map(document.getElementById('map-canvas'), 
     mapOptions); 

    // Try HTML5 geolocation 
    if(navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(function(position) { 
     var pos = new google.maps.LatLng(position.coords.latitude, 
             position.coords.longitude); 


     var marker = new google.maps.Marker({ 
      position: pos, 
      map: map, 
      title: 'You Are Here' 

     }); 

     map.setCenter(pos); 
    }, function() { 
     handleNoGeolocation(true); 
    }); 
    } else { 
    // Browser doesn't support Geolocation 
    handleNoGeolocation(false); 
    } 

} 

有一種方法可以做到這一點?我知道EnableHighAccuracy是可能的:true,但我不知道我把這個命令放在哪裏。任何人都有解決方案?

回答

3

引用形式Geolocation API spec

interface Geolocation { 
     void getCurrentPosition(PositionCallback successCallback, 
           optional PositionErrorCallback errorCallback, 
           optional PositionOptions options) 
     [...] 
    } 

[...]

的enableHighAccuracy屬性提供一個提示,該應用程序想獲得最好的結果。

這意味着可以在一個對象傳遞這個作爲這樣的(最小的例子)最後一個參數:

navigator.geolocation.getCurrentPosition(
    function(position) { console.log(position); }, // Success callback 
    function() {}, // Error callback 
    {enableHighAccuracy: true} // Position Options 
);