Hey,

I am having some trouble with Google Maps API... I have the following code:

function geocodePosition(pos) {
  geocoder.geocode({
    latLng: pos
  }, function(responses) {
    if (responses && responses.length > 0) {
      updateMarkerAddress(responses[0].formatted_address);
    } else {
      updateMarkerAddress('Cannot determine address at this location.');
    }
  });
}

However this shows the full address.... However I only want it to show the Town/City County and Country EG:
York, Yorkshire, England

Can anyone help me change my code to return this result to the function updateMarkerAddress();

Dan

I have not used Google Maps' reverse geocoding but think you will have problems achieving exactly what you want. The reason is that, whereas you may get several results, you have no control over, and no prior knowledge of, the format of their .formatted_address properties.

You may be able to loop through the .address_components array (of each result) and build the address to the required format but, again, you have no prior knowledge of what address component types are present in any particular result.

As they say in the API (admittedly with a different emphasis), "reverse geocoding is not an exact science".

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.