Google Maps Api:使用NearbySearch()获得20个结果,但使用PlacesService()仅获得9个结果

时间:2018-04-07 02:21:08

标签: javascript google-maps-api-3

我正在使用谷歌地图Api,这就是我的工作:

  • 我初始化地图
  • 我用nearbySearch()搜索餐馆。 API给了我20个结果。
  • 我需要评论和评分详情,因此我会使用getDetails()搜索前20个结果中的每一个的详细信息。但API只给了我9个结果......

我只使用文档中的示例...所以我不明白为什么会这样做!

这是我的代码:(查看控制台,地图上什么都没有,这是正常的)

var map;
var service;

// MapInit()
function MapInit() {

    // Initial position, the problem doesn't come from the location, you can test
    var initialPosition = new google.maps.LatLng(48.8589507, 2.2770201); // Paris
    //var initialPosition = new google.maps.LatLng(40.6976701, -74.2598681); // New York
    //var initialPosition = new google.maps.LatLng(35.6735408, 139.5703028); // Tokyo

    // I create the map
    map = new google.maps.Map(document.getElementById('map'), {
        center: initialPosition,
        zoom: 15
    });

    // I prepare the request for nearbySearch()
    var request = {
        location: initialPosition,
        radius: '1000',
        types: ['restaurant']
    };

    // I do the nearbySearch() request to found restaurants around the initial position
    service = new google.maps.places.PlacesService(map);
    service.nearbySearch(request, NearbySearchCallback);

}


// This function is called when nearbySearch() has done
function NearbySearchCallback(results, status) {
    if (status == google.maps.places.PlacesServiceStatus.OK) {

        // 20 results for this search
        console.log("### All results for nearbySearch() ###");
        for (var i = 0; i < results.length; i++) {
            console.log(results[i].name);
        }

        // I do a new search for the details with getDetails(), but it will only return me 9 results instead of 20
        console.log("### All results for getDetails() ###");

        // For each results of the nearbySearch() request
        for (var i = 0; i < results.length; i++) {

            // I prepare the request for getDetails()
            var place = results[i];
            var request = {
                placeId: place.place_id
            };

            // I do the getDetails() request for each results
            service = new google.maps.places.PlacesService(map);
            service.getDetails(request, getDetailsCallback);
        }
    }
}

// This function is called when getDetails() has done
function getDetailsCallback(place, status) {
    if (status == google.maps.places.PlacesServiceStatus.OK) {
        console.log(place.name);
    }
}

这是console.log:

### All results for nearbySearch() ###
Maison FL
Ken Club
Sushi Gourmet
La Gare
Astrance
Café Kléber
Restaurant Bon
LE FRANKLIN Passy
Café de l'Homme
Sushi Shop Mozart
Café du Trocadéro
Château de la Tour
Le Grand Bistro de la Muette
Matsuri Passy
Pizza Hut Annonciation
Domino's Pizza Paris 16 Nord
Le Relais du Bois
Family Café
Settebello
Genio
### All results for getDetails() ###
Astrance
Ken Club
LE FRANKLIN Passy
Sushi Gourmet
La Gare
Restaurant Bon
Café de l'Homme
Maison FL
Café Kléber

我确定我错过了一些东西,但我已经知道了2天,我仍然不明白发生了什么。

如果我的代码完全错误,我的主要问题是:如何从20个结果中获得评论和评分?

非常感谢你的帮助。 :)

1 个答案:

答案 0 :(得分:0)

所有Google的服务都受到费率限制和配额的限制。在执行长期限制(每秒约1次)之前,速率限制将您限制为大约10个结果。检查响应中的status(您当前默默地忽略失败),如果OVER_QUERY_LIMIT您需要放慢请求速度。如果您对用户操作执行详细信息请求(例如点击标记或链接,则不会出现此问题。)