So, since you did not give details of your code, I'll post an explanation on HTML5 that I recently read in the W3C documentation on geolocation.
There are three popular ways to get your position:
IP Geolocation
This is the method used by most Web browsers on computers. Through whois queries and IP location services, it will determine which city or region you are in.
GPRS triangulation
Devices connected to a cellular network and without a GPS, or with the GPS turned off, can determine their position by the triangulation of the next GPRS antennas . It is much more accurate than the IP-based method, it will show where in the neighborhood you are.
GPS
It is the most accurate method. Ideally, the margin of error is only 5 meters.
While these are the three most popular ways to solve the problem, they may not be the only ones. Some user agents may use a combination of these methods, or even a new method that might be invented. So the Geolocation API is agnostic over the method used. There is only one way to turn on and off the "high precision mode", which will have different meaning in each user agent.
Let's take an example:
function getPosition() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(position, erro);
}
}
function position(posicao) {
console.log(posicao.coords.latitude, posicao.coords.longitude);
}
function erro(positionError) {
console.log(positionError.code);
if (positionError.code == 1)
document.getElementById('msg').innerHtml = 'Usuário bloqueou a localização';
}
Where postion
is a callback function, which will receive a positioning object.
The getCurrentPosition
method receives two other parameters. The first is a function for handling the second error, a configuration object.
Error will receive an object with details of the error, which will be explained below.
Running the above code, the browser will display:
The user can then choose whether or not to share their position with the site. Besides the user being able to say no, a lot can go wrong when it comes to geolocation. To handle this, you can pass the second parameter to getCurrentPosition, in our case the callback erro
.
If something goes wrong, the error function will receive a positionError
object, which has the code
attribute, which can have one of the following values:
1- Permission denied
The user clicked do not share.
2- Position unavailable
The user agent is disconnected, GPS satellites could not be reached or some similar error.
3- Timeout
Timeout when getting a position. You can set the maximum time when calling getCurrentPosition.
0- Unknown error
Something else has prevented the user agent from obtaining a position.
Do not treat the user's response as an error, in its error handling function, if you get error code 1, please do not bother the user with error messages. He chose not to share his position with the site. Perhaps the best attitude is to do nothing at the moment.
Configuration object :
The third parameter of getCurrentPosition
is a configuration object, which can have the following properties:
enableHighAccuracy
If true, turn on high precision mode. In a cellular this can instruct the browser, for example, to use GPS instead of GPRS triangulation.
timeout
The time in milliseconds that the user agent will wait for the position before triggering a type 3 error.
maximumAge
The time, in milliseconds, that the browser can cache position.