Best method of discovering the state

Asked

Viewed 762 times

3

I need to find out the status that the user is accessing the page, to select already be marked, and some functions enabled.

I was seeing that finding the status from the IP is pretty flawed. Ai saw that had to use the HTML5 Geolocation, however it only returns the latitude and longitude. Would somehow it return the state ?

I was looking for some function that would return the state, after informing the latitude and longitude, but did not find.

Does anyone know what I can do to precisely get what status the user is accessing ?

1 answer

3


Using HTML5 and Oppenstreetmaps looks like this:

navigator.geolocation.getCurrentPosition(function(posicao) {
    var url = "http://nominatim.openstreetmap.org/reverse?lat="+posicao.coords.latitude+"&lon="+posicao.coords.longitude+"&format=json&json_callback=preencherDados";

    var script = document.createElement('script');
    script.src = url;
    document.body.appendChild(script);
});



function preencherDados(dados) {
  alert(dados.address.state);  
}
  • What is the degree of accuracy ? Can it hit 100% states ? Or is it still preferable I put something to him to confirm the state that he really is ?

  • 1

    In browsers that support HTML5 I believe it is 100%. I’ve never had problems with this code, I’ve even used it on a car rental site all over the country and it always worked. The ideal is that when filling the city, the state or any field that returns there is to make flexible the edition.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.