问题
Can I use embedded Google map in my website to show the name of location if found, and if not found then just the address?
I can get my code to show the address in all cases but the name is showing up only in some cases. I have a few examples in http://www.yankeedesi.com/testmap.htm. If you look at the first row, I can get the map working with address but not with name of the place. In the second row, however both the cases are working.
The code I used for the first one is:
<iframe width="300" height="250" frameborder="0" style="border:0"
src="https://www.google.com/maps/embed/v1/place?key=<MyKey>&q=910+11th+St,Anacortes+WA+98221">
</iframe>
and for the second one is
<iframe width="300" height="250" frameborder="0" style="border:0"
src="https://www.google.com/maps/embed/v1/place?
key=AIzaSyCpmbSHS59rVuyJGtIStR4CcGo2g3dNXF8
&q=Little+Calcutta+Restaurants,910+11th+St,Anacortes+WA+98221">
</iframe>
Now in the second case because Google map is unable to find the address with the name Little+Calcutta+Restaurants, it is not searching the address as well.
What I want is: If the search with name is successful then show it with name, else search with address and ignore the name. Is that possible?
回答1:
yeah use geocoder
var geocoder;
add this to your initialize before making a map
geocoder = new google.maps.Geocoder();
function search(){
var address = document.getElementById('address').value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
//search complete
} else {
//search again but subtring your address
}
来源:https://stackoverflow.com/questions/24944089/google-map-to-show-name-of-location-if-found-otherwise-just-the-address