简体   繁体   中英

Search by Hotel Name in Google Maps v3 API

I'm using the following code to search for addresses and list them on the page (not displaying them on the map at this moment). It works fine, but I'd like to expand it to allow searching for hotels by name. Is that possible?

function showAddress() {
    var geo = new google.maps.Geocoder();
    var address = 'ADDRESS GOES HERE';
    geo.geocode({
        "address": address
    }, function (result, status) {
        var out = 'no results found';
        if(status == google.maps.GeocoderStatus.OK) {
            if(result.length == 1) {
                out = "Result found: " + result[0].formatted_address + "</a>";
            }
            else {
                out = "Did you mean:";
                for(var i = 0; i < result.length; ++i) {
                    out += "<br>" + (i + 1) + ": " + result[i].formatted_address;
                }
            }
        }
        document.getElementById("message").innerHTML = out;
    });
}

From a logical point of view, it should be possible by putting something else instead of 'address' in the geocoder function but I'm having trouble finding what exactly that would be. Can anyone help with this?

You are using Geocoding service which is used to translate an address to geographical coordinates (lat/lng) or reverse. You are probably looking for Google Maps Places API which allows you to search for objects (such as hotels by keyword/name).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM