Address suggestions by google maps

2019-02-07 10:01发布

问题:

Does somebody know if there is any way to reproduce an ajax suggestion box like http://maps.google.com/ have in my webpage using the google maps api?

The idea would be for example if somebody writes down "15 Avenue" a list of suggestions:

"15 Avenue of the Americas, New York, NY, United States"

"15 Avenue of the Stars, Los Angeles, CA, United States"

Etc

Just a yes no qustion =)

Thanks

回答1:

Yes.

Look at the geocode response for "15 Avenue"...

http://maps.googleapis.com/maps/api/geocode/json?address=15+Avenue&sensor=false

You get a list of possible results that you can put in a dropdown.



回答2:

There is an API example from Google.

Make sure you load the places library:

<script type="text/javascript"
    src="https://maps.googleapis.com/maps/api/js?v=3&libraries=places">
</script>

Next your javascript:

var autocomplete;
function initialize() {
    autocomplete = new google.maps.places.Autocomplete(
        document.getElementById('autocomplete'),
        { types: ['geocode'] }
    );
}
google.maps.event.addDomListener(window, 'load', initialize);

With HTML

<div><input id="autocomplete" type="text"></div>

Also see https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform



回答3:

I found this on another answer on stackoverflow. This page shows how to provide autocomplete choices using google maps/geocoding.

http://tech.cibul.net/geocode-with-google-maps-api-v3/