Google Maps API Tutorial - Did You Mean?

© 2007, 2008, 2009 Mike Williams

Translate this page:


"Did You Mean?"

The page will ask a "Did you mean?" question in response to an address search under certain circumstances. You might possibly want to offer the same functionality within the API.

Note: The API geocoder is quite different from the geocoder. Situations which cause the conditions which provoke a "Did you mean?" in one geocoder will not usually cause those conditions in the other geocoder.

Multiple Hits

One condition which provokes the question is when the geocoder returns more than one Placemark in response to the query.

To detect this situation with the API geocoder, simply test whether result.Placemark.length is greater than 1.
If it is greater than 1, then display a list of clickable options, using the Placemark.address values that are returned. Store the corresponding Placemark.coordinates so that they can be used to plot the marker when the user selects one of the alternatives.

Here's an example

The API geocoder doesn't often return multiple results. Bispham Road, Bispham, UK is one example address which currently produces three results in the API geocoder (because there really are three Bispham Roads in Bispham).

Potential Pitfalls

  1. Don't store the Placemark.address and try to geocode that when the user selects it.
    Even though the geocoder returns that address, it doesn't mean that it can geocode it if you pass it back.

Significantly Different Address

The geocoder may occasionally return an address which is sufficiently different from the requested address to be worth asking the "Did you mean?" question.

For example, if you search for Bespham Road, Cleveleys, FY2, the API geocoder will return a single result for Bispham Rd, Blackpool, FY2 0, United Kingdom, which you might consider to be sufficiently different to be worth querying.

This circumstance is much harder to test for. The returned address will often look very different from the query just because the geocoder converts the address into a standard format. In this case, the fact that "Gilford" has been replaced by the zip code "03249"; and the fact that ",USA" has been added; should not be considered to be "significant" differences in the address.

I reckon that it's impractical to compare anything other than the house number and street. To go any further would require some fairly sophisticated AI to handle all the possible valid variations that might be used, and you'd almost need to write your own geocoder to check if "Gilford" actually is zip code "03249".

My strategy for matching the street is to

  • Write a little function that converts words like "street", "lane", "north" etc. into standard versions "st", "la", "n" etc.
  • Split the reply at the first comma to obtain the part that specifies the street.
  • Convert the query and the reply to the same case, e.g. .toLowerCase().
  • Remove any apostrophes, so that "St Fred's Rd" matches "St Freds Rd".
  • Replace any other punctuation with spaces, since the geocoder ignores punctuation except that it separates words.
  • Replace multiple spaces with a single space, so that we now have a string of words separated by single spaces.
  • Split the strings into arrays of words.
  • Convert each word to the "standard" version.
  • Compare corresponding words from the query and the reply
Here's an example

I've not done an awful lot of testing with this.

Back to Mike's Homepage
Up to the start of this tutorial