0

我正在尝试查询 Google Places API,向其传递原点位置和半径以及一些关键字。这曾经奏效!直到最近我才注意到结果的质量变得更差了,从我的研究来看,如果我在搜索框中提供两个或更多单词,似乎没有返回任何结果。

我准备了一个示例 HTML 文件来演示该问题。我们正在美国印第安纳州印第安纳波利斯附近进行搜索,以查看有什么问题,在搜索框中键入Indiana ,然后单击“搜索”。这应该返回一些结果。其中之一是印第安纳州立博物馆。让我们缩小搜索范围,输入Indiana State,我们应该会在结果列表中看到 Indiana State Museum,好吧……哇!它去哪儿了?:( 谷歌以这种方式返回 ZERO_RESULTS。我做错了什么还是他们的 API 发生了变化,变得几乎没用了?

下面是我的代码,如果你喜欢,你也可以抓住要点。

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=places&amp;sensor=false"></script>
    </head>
    <body>
        <input type="text" id="search" style="width: 300px;" /><input type="button" id="searchBtn" value="Search"/><br/>
        <div id="map" style="width: 300px; height: 300px;"></div>
        <div id="results" style="width: 600px;height: 600px;background-color:gray;color:white;"></div>
        <script type="text/javascript">
            $(function(){
                var indy = new google.maps.LatLng(39.7685825,-86.1579557);

                var map = new google.maps.Map(document.getElementById('map'), {
                    mapTypeId: google.maps.MapTypeId.ROADMAP,
                    center: indy,
                    zoom: 10
                });

                var service = new google.maps.places.PlacesService(map);

                $('#searchBtn').click(function() {
                    $('#results').empty();
                    var request = {
                        location: indy,
                        radius: 5000,
                        /*types: ['geocode'],*/
                        name: $('#search').val()
                    };
                    service.nearbySearch(request, function(results, status) {
                        if (status == google.maps.places.PlacesServiceStatus.OK) {
                            printLocationName($.map(results, function (item) {
                                return {
                                    reference: item.reference,
                                    label: item.formatted_address,
                                    value: item.name,
                                    latitude: item.geometry.location.lat(),
                                    longitude: item.geometry.location.lng()
                                };
                            }));
                        }
                        if (status == google.maps.places.PlacesServiceStatus.ZERO_RESULTS) {
                            $('#results').append('<span>No results.</span><img src="http://i474.photobucket.com/albums/rr104/gio_dim/AVerySadPanda.png"/>')
                        }
                    });
                });

                function printLocationName(places) {
                    for (var i = 0; i < places.length; i++) {
                        $('#results').append('<span>' + places[i].value + '</span><br/>');
                    }
                }
            });
        </script>
    </body>
</html>
4

2 回答 2

0

'name' 参数用于非常具体的搜索。只需将“名称”更改为“关键字”即可。

于 2013-02-15T05:41:02.540 回答
0

看起来这是API中的一个错误。:(

http://code.google.com/p/gmaps-api-issues/issues/detail?id=4845

http://code.google.com/p/gmaps-api-issues/issues/detail?id=4933

于 2013-02-21T14:13:20.410 回答