Different results in Google Places API and Google

2019-09-02 15:59发布

I'm using the Google Places JavaScript API for a desktop application. However, the search results returned by the API aren't the same as the ones I get in maps.google.com. For instance, if I search for "Antique Hostel", I get many results (almost all of them are kinda random) whereas on Google Maps I get the correct (single) result. Is the quality of the search isn't the same in the API and the service? Here is my code

function initialize() {
    // map setup
    var mapOptions = {
        center: new google.maps.LatLng(52.519171, 13.406091199999992),
        zoom: 6,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    },
        $map = document.getElementById('mapCanvas'),
        map = new google.maps.Map($map, mapOptions),
        input = document.getElementById('searchInput'),
        autocomplete = new google.maps.places.Autocomplete(input),
        service = new google.maps.places.PlacesService(map),
        $ajaxSearchInput = $(input),
        markers = [];

    autocomplete.bindTo('bounds', map);
    $ajaxSearchInput.keydown(function(e) {
        if (e.keyCode === 13) {
            var request = {
                query: $(this).val(),
                radius: '500',
                location: map.getCenter()
            }
            service.textSearch(request, searchCallBack);
        }
    });

    searchCallBack = function(results, status) {
        if (status == google.maps.places.PlacesServiceStatus.OK) {
            var bounds = new google.maps.LatLngBounds();
            console.log(results);
            for (var i = 0, marker; marker = markers[i]; i++) {
                marker.setMap(null);
            }
            for (var i = 0; i < results.length; i++) {
                var place = results[i];
                createMarker(results[i], bounds);
            }
            map.fitBounds(bounds);
        }
    }

    createMarker = function(place, bounds) {


        var marker = new google.maps.Marker({
            map: map,
            title: place.name,
            position: place.geometry.location
        });

        // event listener to show the InfoWindow.
        (function(marker, place) {
            google.maps.event.addListener(marker, 'click', function() {
                var content = '<h3>' + place.name + '</h3> ' + place.formatted_address + '<br>',
                    infowindow = new google.maps.InfoWindow({
                        content: ''
                    });

                if (place.rating) {
                    content += '<p> <b>Rating</b>: ' + place.rating + '</p>';
                }
                if (place.types) {
                    content += '<p> <b>Tags</b>: ';
                    for (var j = 0, tag; tag = place.types[j]; j++) {
                        content += tag + ', '
                    }
                    content += '</p>';
                }
                infowindow.content = content;
                infowindow.open(map, marker);
            });
        })(marker, place);

        markers.push(marker);
        bounds.extend(place.geometry.location);
    }
}

google.maps.event.addDomListener(window, 'load', initialize);

0条回答
登录 后发表回答