4

I'm using Google Maps V3 API. Whenever a user drops a pin on a street (or perhaps a few metres/yards away from a street) it should get the address components which I use to retrieve from the dropped marker.

What is bothering me, is that when I drop a pin on a street, sometimes it returns the house number/name when it should always return the list as:

  • Street name;
  • City;
  • State;
  • County;
  • then Country

I go through the address components via a custom made that returns the entire JSON response generated from the Google Maps API:

getAddress(pinLocation, function(addressobj)
{
    for(i = 0; i < addressobj[0].address_components.length; i++)
    {
        var addressComp = addressobj[0].address_components[i].long_name;
        $('input[type=text][address=comp]:eq(' + i + ')').val(addressComp);
    }
});

So when the data is returned it returns results and each address component (from the list above) each goes into a input field. This is what kind of expected results returns:

  • San Antonio Valley Rd (street name)
  • Livermore (city)
  • Diablo Range (state)
  • Santa Clara (county)
  • California (country)

This is the perfect response but from some locations when dropping on a street (mostly crowded streets) I get like:

  • 2920 (should be Dalarna Ct)
  • Dalarna Ct (should be Turlock)
  • Turlock (this is okay, but is omitted)
  • Turlock (should be Stanislaus)
  • Stanislaus (should be California)

I have no idea how I can make a foolproof address component that does not display the house number, and should always return the information regarding the list (first one) because the data always varies when dropping markers on a street when I need it to produce the same results as the list.

Matt
  • 22,721
  • 17
  • 71
  • 112
MacMac
  • 34,294
  • 55
  • 151
  • 222
  • Would it be possible to show how you're fetching the JSON? And is it possible to provide a link to the app so we can see this behavior in action? – Trott May 28 '11 at 15:17
  • Why is it "the perfect response" for California (the state) to end up in the country field, and for Diablo Range (not a state) to end up in the state field? Shouldn't the state be California and the country be United States? Is that just a typo? – Trott May 28 '11 at 21:16

2 Answers2

3

Use this for your getAddress function:

    geocode2FormFields = {'route':0,
                          'locality':1,
                          'sublocality':1,
                          'administrative_area_level_1':2,
                          'administrative_area_level_2':3,
                          'country':4};

    for(i = 0; i < addressobj[0].address_components.length; i++) { 
        for(j = 0; j < addressobj[0].address_components[i].types.length; j++) {
            formFieldIndex = geocode2FormFields[addressobj[0].address_components[i].types[j]];
            if (typeof formFieldIndex !== 'undefined') {
                var addressComp = addressobj[0].address_components[i].long_name;
                $('input[type=text][address=comp]:eq(' + formFieldIndex + ')').val(addressComp);
            }
        }
    }

As Google's documentation says, "Reverse geocoding is not an exact science." However, I believe this should provide reasonable results for most places in the United States. Your field names (e.g., "state") seem to assume a United States location, so I'm guessing that will meet for your needs, or at least be closer to ideal than what you have now.

If you ever find you want or need to tweak the geocode2FormFields stuff, the various types are documented under "Address Component Types" at http://code.google.com/apis/maps/documentation/javascript/services.html.

Trott
  • 66,479
  • 23
  • 173
  • 212
  • This is not just for USA. The application will be used globally from around the world. This is what I mean by a state: http://en.wikipedia.org/wiki/Nation_state – MacMac May 28 '11 at 20:56
  • My code should be easily modifiable to accommodate whatever your situation. Just review the Address Component Types part of the linked doc and go from there. I would adjust it to accommodate your situation except that what you're describing doesn't make a whole lot of sense to me. If you are thinking of a "state" as a "nation state", why would "Diablo Range" be the correct expected result in your first example? That doesn't make any sense to me. And I can't think of a reasonable situation wherein "country" should be California rather than United States....Regardless, try my code anyway! – Trott May 28 '11 at 21:10
  • Hmmm. Our opinion differs, I can't blame you for that nor myself because I am a British citizen and we have different types of views when it comes to location types such as categorising the state/country of a location. I have tried your code, which is pretty good and your bounty will be awarded in 5 hours. – MacMac May 28 '11 at 21:15
  • Interesting! It is indeed likely, probably inevitable, that the whole thing will need some tweaking when you cross national boundaries. I wonder if there is some effort to standardize address formats/nomenclature worldwide. – Trott May 28 '11 at 21:20
0

Each of those address_components, in addition to having "long_name" property, has a "types" property which is an array of, well, types. Here are some of the types: street_number route (street name) postal_code and many more.

Take a look at http://code.google.com/apis/maps/documentation/places/#PlaceDetails

James
  • 20,957
  • 5
  • 26
  • 41
  • I've also tried that. It seems unreliable though. I get incorrect responses that I've matched with the types. – MacMac May 27 '11 at 21:58