Analyze google geocode

I am using google geocoding api in my application, but I am having a hard time parsing the returned address information. I hope someone has a library or way of parsing the information so that I can't write this myself and cover every case.

I'm using the json api and can extract the address bar and coordinates quite easily and store them in the database, but the problem is you are retrieving other address information in a reliable way so that I can store it.

I looked at the XAL structured address spec that Google uses for geocoded data, but my problem is that it can serve every type of address in every country, making it difficult to parse the result.

Something as simple as a zip code can be nested quite deep within any combination of parent tags. even within one country, the way the tree is displayed can vary, making it more difficult than expected to parse.

Ironically, my ultimate goal is to store it in a tree (think rails act_as_tree or similar) in my database so that users can easily find items later. for example, click country, state, then region, city, then suburb, etc.

I'm running on rails

+2


source to share


2 answers


There is a gem and plugin that encapsulates it all for you. It was called GeoKit .

You can use Google or several other geocoding service providers. If your models have matching fields, it can do distance calculations as well (a very neat thing - the math for this is done in SQL).



I've been using it for a long time in one of my production applications to calculate distances between zipcodes, for example. As I mentioned, I've been using it in production for quite some time, so I don't have any issues with the validity of the code.

+2


source


I've done this a couple of times. Their (Google) sample code works, but it's fragile, and the geocoding data you get from Google is incomplete at best and inconsistent at worst.

Anyway. All geocoders suck, but at least Google is free and sucks less than others. Is it too much to ask for consistency? Sigh. Try this, you pass a Placemark to it (note that the AJAX getLocations method can return multiple) and it normalizes the result. If you want to get fancy, use the placemark.address field - sometimes it will contain more information than individual fields. Usually it will only contain the text you submitted to be geocoded.



function Mark(placemark) {
  this.latitude = 0.0;
  this.longitude = 0.0;
  this.accuracy = 0;
  this.country = null;
  this.region = null;
  this.city = null;
  this.postalCode = null;
  this.street = null;
  this.address = null;
  this.placemark = placemark;

  if (placemark) {
    if (placemark.Point && placemark.Point.coordinates) {
      this.latitude = placemark.Point.coordinates[0];
      this.longitude = placemark.Point.coordinates[1];
    }

    var elem = placemark.AddressDetails;

    if (elem) {
      this.accuracy = elem.Accuracy;

      if (elem.Country) {
        elem = elem.Country;
        this.country = elem.CountryNameCode;
      }

      if (elem.AdministrativeArea) {
        elem = elem.AdministrativeArea;
        this.region = elem.AdministrativeAreaName;
      }

      if (elem.SubAdministrativeArea) {
        elem = elem.SubAdministrativeArea;
      }

      if (elem.Locality) {
        elem = elem.Locality;
        this.city = elem.LocalityName;
      }

      if (elem.PostalCode) {
        this.postalCode = elem.PostalCode.PostalCodeNumber;
      }

      if (elem.Thoroughfare) {
        this.street = elem.Thoroughfare.ThoroughfareName;
      }
    }
  }
}

      

+1


source







All Articles