Getting the exact marker address - Google Maps V3

I am using the Google Maps V3 API. Whenever the user throws a pin onto the street (or perhaps a few meters / yards from the street), he should get the address components that I use to extract from the reset marker.

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

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

I look at the components of the address through custom code that returns the entire JSON response created in 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); } }); 

Therefore, when data is returned, they return results, and each component of the address (from the list above) each goes to the input field. Here's what the expected result returns:

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

This is the perfect answer, but from some places, when it falls onto the street (mostly crowded streets), I get the following:

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

I do not know how I can create a reliable address component that does not display the house number, and should always return list information (first), because the data always changes when street markers are deleted, when I need it to get the same results as the list.

+4
source share
2 answers

Use this for the 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 the Google documentation says: "Reverse geocoding is not an exact science." However, I believe that this should provide reasonable results for most places in the United States. Your field names (for example, "state") seem to suggest a location in the United States, so I assume this will suit your needs or at least be closer to ideal than what you have now.

If you ever need or need to customize the geocode2FormFields material, the different types are documented in the "Types of Address Components" section of http://code.google.com/apis/maps/documentation/javascript/services.html .

+3
source

Each of these address_components attributes, in addition to the 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) Zip code and more.

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

0
source

All Articles