i need to use geolocation in my website for the banner.
I found this library which seems to be good:
In my page i added this code and the browser shows me the alert to allow to get the position.
Code:
<script type="text/javascript">
if(geo_position_js.init()){
geo_position_js.getCurrentPosition(success_callback,error_callback,{enableHighAccuracy:true});
}
else{
alert("Functionality not available");
}
function success_callback(p)
{
var latitudine = +p.coords.latitude.toFixed(2);
var longitudine = p.coords.longitude.toFixed(2);
alert(latitudine+' - '+longitudine);
}
function error_callback(p)
{
alert('error='+p.message);
}
</script>
I can see the alert without problem but i need to get the city to load different banner.
How can I do that?
I also found this service: findNearbyPlaceName
which allow to find country etc from the latitude and longitude, but how can I use this information? I see that they have a callback with xml or json, i tried to get the url with this code but it is wrong cause browser doesn’t show the alert:
<script type="text/javascript">
if(geo_position_js.init()){
geo_position_js.getCurrentPosition(success_callback,error_callback,{enableHighAccuracy:true});
}
else{
alert("Functionality not available");
}
function success_callback(p)
{
var latitudine = +p.coords.latitude.toFixed(2);
var longitudine = p.coords.longitude.toFixed(2);
$.getJSON(
'http://http://api.geonames.org/findNearbyPlaceNameJSON?lat='+latitudine+'&lng='+longitudine,
function(data) {
alert(data);
}
);
}
function error_callback(p)
{
alert('error='+p.message);
}
</script>
What you need is a “reverse geocoder”.
With a reverse geocoder you can get the city name (even the street name) for particular coordinate.
A very simple way would be to use Google’s Geocoder available in Google Maps API V2 and V3:
Example (Source): https://developers.google.com/maps/documentation/javascript/geocoding#ReverseGeocoding
Hope this helps!