2

This question may look familiar: I have the latitude and longitude of a place. I need to get the name of country. I know I have to use reverse geo coding for this. But my problem is that sometimes it returns the short form of the area or country (for example US for United State or CA for California). Is there any way that I can get the full name of the country? I can't perform a match operation by this short forms with my prestored country database.

I have already gone through this, this. But it's not much help for my problem.

Community
  • 1
  • 1
arjuncc
  • 3,227
  • 5
  • 42
  • 77

4 Answers4

5

The geocoder response usually returns several results which include street corners, intersections, counties, and other alternate representation names. I found that results[0] is the best description.

The trick is searching for "country" in the results. Then the long_name can be retrieved.

Click on the map

  function getCountry(latLng) {
    geocoder.geocode( {'latLng': latLng},
      function(results, status) {
        if(status == google.maps.GeocoderStatus.OK) {
          if(results[0]) {
            for(var i = 0; i < results[0].address_components.length; i++) {
              if(results[0].address_components[i].types[0] == "country") {
                alert(results[0].address_components[i].long_name);
              }
            }
          }
          else {
            alert("No results");
          }
        }
        else {
          alert("Status: " + status);
        }
      }
    );
  }
Heitor Chang
  • 6,038
  • 2
  • 45
  • 65
  • 1
    thanks a lot sir. I just had one day behind this. Your answer saved a lot of time – arjuncc May 23 '12 at 04:20
  • Hi, I have tried to get the street name using your code. I have replaced "country" in the types with "street_address" but it doesn't seem to work. Is there a chance you could help me with this? Thanks! – inadcod Oct 11 '12 at 15:06
  • Found it. I just had to replace "country" with "route" :) – inadcod Oct 12 '12 at 07:48
2

The JSON array normally includes both long_name and short_name. You should be able to extract both...

dda
  • 6,030
  • 2
  • 25
  • 34
0

Here is an JSON/XML parser that works for both google street maps and open street maps.

(the only problem is that it requires a JSON or XML object as the "reply" its tested on version 3 google and 0.6 open street maps and it works good)

NOTE: it returns an object location.lat or location.lon you can also have it return whatever other field you want.

JSON.parse(text) // where text is the reply from google or open street maps XML.parse(text) // you can make your own to convert the reply to XML or use regex to parse it. If someone has a regex version to parse the text reply that may also be helpful.

// Parser(ajax reply object, google/open, json/xml); 
// takes the reply from google maps or open street maps and creates an object with location[lat/lon] 
function Parser(reply, provider, type) {
  var location = {};
  if(reply != null) {
    if(provider == "google") { // Google Street Maps
      switch(type) {
      case "xml":
        location["lat"] = reply.getElementsByTagName("lat")[0].textContent; 
        location["lon"] = reply.getElementsByTagName("lng")[0].textContent; 
        break;
      default: // json
        location["lat"] = reply.results[0].geometry.location.lat;
        location["lon"] = reply.results[0].geometry.location.lng;
      }
    }
    else { // Open Street Maps
      switch(type) {
      case "xml":
        location["lat"] = reply.getElementsByTagName("place")[0].getAttribute("lat"); 
        location["lon"] = reply.getElementsByTagName("place")[0].getAttribute("lon"); 
        break;
      default: // json
        location["lat"] = reply[0].lat;
        location["lon"] = reply[0].lon;
      }
    }
  }
  return location;
}
Asher
  • 2,638
  • 6
  • 31
  • 41
0
function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function (position) {
            $.post("https://maps.googleapis.com/maps/api/geocode/json?latlng=" + position.coords.latitude + "," + position.coords.longitude + "&sensor=false", function (result) {
                for (var i = 0; i < result['results'][0]['address_components'].length; i++) {
                    if (result['results'][0]['address_components'][i]['types'][0] == "country") {
                        alert(result['results'][0]['address_components'][i]['long_name']);
                    }
                }
            });
        });
    }
}
getLocation();
Uday Hiwarale
  • 4,028
  • 6
  • 45
  • 48