Is there any gmap’s api function to concatenate address string from AddressDetails structure?
I’am using Google Map’s GClientGeocoder for reversing map coordinates into string address. Exactly as shown in google’s example here http://code.google.com/apis/ajax/playground/?exp=maps#geocoding_reverse
But, I would like to remove LocalityName (place.AddressDetails.Country.AdministrativeArea.Locality.LocalityName) from place.address and have the address string without any LocalityName.
The straight way will be join all AddressDetails elements, excluding LocalityName. However order of the structure elements in final string representation is depends from geographical location.
For example:
Order for Australia ci开发者_如何学JAVAty:
ThoroughfareName + “, ” + LocalityName + “ ” + AdministrativeAreaName + “ ” + PostalCodeNumber + “, ” + CountryName
Order for Russian city:
CountryName + “, ” + PostalCodeNumber + “, ” + LocalityName + “, ” +ThoroughfareName
Moreover PostalCodeNumber was not supplied in AddressDetails for the last example.
Please, help!
Get the LocalityName and make a new string without it. As LocalityName is buried in an unknown way within the JSON structure, you need to go through the whole AddressDetails recursively until LocalityName is found. Example:
function findLocalityName(item) {
for (key in item) {
if (key == "LocalityName") return item[key];
else if (item[key] instanceof Object) return findLocalityName(item[key]);
}
}
var place = response.Placemark[0];
var localityName = findLocalityName(place.AddressDetails);
var noLocalityAddress = place.address.replace(localityName, '');
Working example:
http://savedbythegoog.appspot.com/?id=6da11815a8a7435cc971acc9cebe99f4732fc5d0
Click anywhere in the map to show full address, LocalityName and fixed address.
This could use some minor refinements, i.e. if you click "Redfern St." in the example, it will remove the street name instead of the locality name, because the street name is the same as the locality!
精彩评论