views:

54

answers:

2

When using Googles geocoder service to display a city on a map; filling out a non-existing city results in an error.

Is there a way to display some suggestions based on the filled out city?

var geocoder = new GClientGeocoder();
function showAddress(address, zoom) {

          geocoder.getLatLng(
           address,
           function(point) {
            if (!point) {
                  //no point found....
              //Suggest some points :)
            } else {
                map.setCenter(point, zoom);

            }
          }
         );
        }
showAddress('Someplace, Nederland', 14);
A: 

If it finds a match it will return the matches as Addresses and those are the suggestions. It shouldn't return an error, it should just return an empty list if it finds no matches. Can you post the code?

BrennaSoft
I've added the code to my initial post.
richardverbruggen
A: 

If you are simply geocoding cities, you may want to consider starting to build your own cities-to-coordinates cache.

This is one approach that you may want to consider:

  • Prompt the user to enter a city name.
  • First issue an AJAX request to your server to check if that city name is present in your cache.
  • If it is, proceed your JavaScript logic, as if you obtained the coordinates from the Google Geocoder.
  • If the city name was not present in your cache, send a request to the Google Geocoder, as you are currently doing.
  • If the Google Geocoder returns a positive result, issue another AJAX request to your server in order to store this city and its coordinates in your cache. You will never ask Google again for the coordinates of this city. Then proceed normally within your JavaScript application.
  • If the Google Geocoder returns "Unknown address" tell the user that this city name was not found, and suggest to retry using a more common city name. Keep note of the original city name that was attempted. Test the second attempt with the Google Geocoder, and if it succeeds, save into your cache both synonyms of the city name (the first one attempted, and the second on that succeeded if not already present).

With this approach you can also seed your cache, in such a way to resolve city names which you are already aware that Google is not able to geocode.

Daniel Vassallo
A good and sound approach. I'm going to implemented this. But how can i then suggest other cities? Like i search on Endhoven, this cannot be found, it should suggest Eindhoven or Meerhoven and so on.
richardverbruggen
@richardverbruggen: The first time a user searches for `Endhoven`, the system can ask him to enter a more common name. The user enters `Eindhoven`, and Google finds it. You should store in your cache both `Endhoven` and `Eindhoven` pointing to the same lat/long. Then the next time a user searches for `Endhoven`, your cache would return the result immediately, without even hitting Google. Eventually, your cache will contain many the synonyms for each city. This will work best if your application will be localized (ie. only in the Netherlands)...
Daniel Vassallo
... It would still work globally, but may take a while for the cache to get populated with enough synonyms.
Daniel Vassallo
All right. Thanx for the input and strategy. Now comes the fine task to implement it :)
richardverbruggen