Michael
Michael

Reputation: 324

Increase accuracy of GPS (in HTML5 geolocation)

How can increase the accuracy of GPS in this code?

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);
  }

}

There is a way to do that? I know that is possible with EnableHighAccuracy: true, but I don't know where I put this command. Anyone have a solution?

Upvotes: 3

Views: 2571

Answers (1)

wonderb0lt
wonderb0lt

Reputation: 2053

Citing form Geolocation API spec:

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

[...]

The enableHighAccuracy attribute provides a hint that the application would like to receive the best possible results.

This means you can pass this in an object as the last parameter like this (minimal example):

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

Upvotes: 6

Related Questions