Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
187
rated 0 times [  191] [ 4]  / answers: 1 / hits: 64644  / 11 Years ago, wed, april 24, 2013, 12:00:00

I am tring to locate a device using the embedded GPS (like whats app share location). I've read that it is possible with enableHighAccuracy: true.



How can I set enableHighAccuracy: true in this code? I tried in different positions but it doesn't work.



<script type=text/javascript>
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var accuracy = position.coords.accuracy;
var coords = new google.maps.LatLng(latitude, longitude);
var mapOptions = {
zoom: 15,
center: coords,
mapTypeControl: true,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.SMALL
},
mapTypeId: google.maps.MapTypeId.ROADMAP
};

var capa = document.getElementById(capa);
capa.innerHTML = latitude: + latitude + , longitude: + , accuracy: + accuracy;

map = new google.maps.Map(document.getElementById(mapContainer), mapOptions);
var marker = new google.maps.Marker({
position: coords,
map: map,
title: ok
});
});

} else {
alert(Geolocation API is not supported in your browser.);
}

</script>

More From » html

 Answers
53

You need a PositionOptions object, in which you set the high accuracy flag following the API.



I am quoting from here: http://diveintohtml5.info/geolocation.html




The getCurrentPosition() function has an optional third argument, a
PositionOptions object. There are three properties you can set in a
PositionOptions object. All the properties are optional. You can set
any or all or none of them.




POSITIONOPTIONS OBJECT

Property Type Default Notes
--------------------------------------------------------------
enableHighAccuracy Boolean false true might be slower
timeout long (no default) in milliseconds
maximumAge long 0 in milliseconds


So, it should work like this:



if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var accuracy = position.coords.accuracy;
var coords = new google.maps.LatLng(latitude, longitude);
var mapOptions = {
zoom: 15,
center: coords,
mapTypeControl: true,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.SMALL
},
mapTypeId: google.maps.MapTypeId.ROADMAP
};

var capa = document.getElementById(capa);
capa.innerHTML = latitude: + latitude + , longitude: + , accuracy: + accuracy;

map = new google.maps.Map(document.getElementById(mapContainer), mapOptions);
var marker = new google.maps.Marker({
position: coords,
map: map,
title: ok
});

},
function error(msg) {alert('Please enable your GPS position feature.');},
{maximumAge:10000, timeout:5000, enableHighAccuracy: true});
} else {
alert(Geolocation API is not supported in your browser.);
}


Noticed I added the following 2 parameters to getCurrentPosition call:




  1. function error(msg){alert('Please enable your GPS position future.');}



    This function is called when GPS could not be retrieved or the timeout has been triggered.


  2. {maximumAge:10000, timeout:5000, enableHighAccuracy: true});



    These are the options. We don't want gps data that is older than 10 seconds (maximumAge:10000). We don't want to wait longer than 5 seconds for a response (timeout:5000) and we want to enable high accuracy (enableHighAccuracy: true).




Also see: Geolocation HTML5 enableHighAccuracy True , False or Best Option?


[#78645] Tuesday, April 23, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
wilson

Total Points: 27
Total Questions: 93
Total Answers: 93

Location: Tajikistan
Member since Sun, Aug 29, 2021
3 Years ago
wilson questions
Tue, Aug 9, 22, 00:00, 2 Years ago
Wed, May 11, 22, 00:00, 2 Years ago
Wed, May 20, 20, 00:00, 4 Years ago
Wed, May 13, 20, 00:00, 4 Years ago
;