Christian Giupponi
Christian Giupponi

Reputation: 7618

Geolocation with geo.js

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>

Upvotes: 0

Views: 2834

Answers (2)

libjup
libjup

Reputation: 4089

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:

  var geocoder;

  function codeLatLng(latitude, longitude) {
    var
        geocoder = new google.maps.Geocoder(),
        latlng = new google.maps.LatLng(latitude, longitude);

    geocoder.geocode({'latLng': latlng}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        if (results[1]) {
          // results[1].formatted_address contains the city name
        }
      } else {
        alert("Geocoder failed due to: " + status);
      }
    });
  }

Example (Source): https://developers.google.com/maps/documentation/javascript/geocoding#ReverseGeocoding

Hope this helps!

Upvotes: 2

Stan Wiechers
Stan Wiechers

Reputation: 2092

Geonames.org operates webservices that among other things lets you lookup the city of a lat/lon coordinate:

http://www.geonames.org/export/web-services.html#findNearbyPlaceName

Supports both xml & json. I can recommend them, does not need any additional javascript download.

Upvotes: 0

Related Questions