Google Maps API:20 个结果与 nearSearch() 但只有 9 个结果与 PlacesServic



我正在使用Google Maps API,这是我做的:

  • 初始化地图
  • 我用附近的搜索((搜索餐馆。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);
    }
}

这是控制台.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 个结果中获得评论和评级?

非常感谢您的帮助。 :)

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

最新更新