开发者

Suggestions based on unknown address in google's geocoder

When using Googles geocoder service to display a city on a map; filling o开发者_开发问答ut 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);


Suggestions are returned in the Placemarks attribute of the results. I'm using v3 and this works, try calling this with address="Washington Blvd"

if (geocoder) {
                geocoder.getLocations(
                address,
                function (results) {
                    if (!results) {
                        alert(address + " no suggestions");
                    } else {
                        $('#output').html('');
                        if (results.Placemark) {
                            for (var i = 0; i < results.Placemark.length; i++) {
                                $('#output').append(i + ': ' + results.Placemark[i].address + '<br/>');
                            }
                        }
                    }
                });
            }


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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜