Reputation: 2915
I am using the following to get the location of a handset..
<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);
}
});
}
}
The result is never derived from the handsets GPS. It does ask for permission to track the position. It does return random locations around the local area.
Why is this?
Upvotes: 0
Views: 152
Reputation: 2915
Simple answer, no answer, all phones treat this differently. the only way i have found to force gps is in a native app
Upvotes: 1
Reputation: 3630
Most smartphones have more than one means of positioning themselves, e.g. GPS for high-accuracy (fine) positioning and cell tower location for low-accuracy (coarse) positioning. On the position returned in your success function, you should get an estimate of the accuracy in coords.accuracy
.
When making the position query, you can request high-accuracy response by specifying enableHighAccuracy = true
:
navigator.geolocation.getCurrentPosition(geo_success, geo_error,
{enableHighAccuracy = true});
Note that this might not always work, as some devices will not allow you to access high-accuracy positioning (for several considerations, one of which being that GPS positioning has an impact on battery use).
Upvotes: 0
Reputation: 163438
That depends on the device. You will find that many devices do in fact return the correct position.
What you are likely noticing is that when the GPS is initialized, it does not have a good fix from satellites, and is giving you the last-known position, a position based on network data, or a position based on limited satellite data.
It is also possible to configure some devices to only share a "coarse" location, which is not as specific or as accurate as it could be.
Upvotes: 0