Getting pinpoint address of marker - Google Maps V3
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 botheri开发者_开发技巧ng 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.
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.
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
精彩评论