首页 > 解决方案 > Google Places API field filter not applying

问题描述

I'm requesting a Google Places data with the "fields" parameter filled in as in example from google: https://developers.google.com/places/web-service/details#fields I am only requesting 3 fields: geometry, name, vicinity.

In the documentation it is clearly stated you should provide at least 1 field: https://developers.google.com/places/web-service/details#fields

function requestGooglePlaces(placeType) {
    var request = {
        fields: ['name', 'geometry', 'vicinity'],
        location: location,
        radius: '5000',
        type: [placeType],
    };

    service = new google.maps.places.PlacesService(map);
    service.nearbySearch(request, callback);
}

function callback(results, status) {
    if (status == google.maps.places.PlacesServiceStatus.OK) {
        for (var i = 0; i < results.length; i++) {
            createMarker(results[i]);
        }
    }
}

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

    google.maps.event.addListener(marker, 'click', function () {
        console.log(place);
        var contentInfoWindows = (place.name + "<br>" + place.vicinity + "<br>" + place.geometry.location);

    });

In my Javascript output console I am receiving way too much fields:

{geometry: {…}, icon: "https://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png", id: "5ffe179c6015e35e796ff313a35ff5149efd396d", name: "Carrefour market", opening_hours: {…}, …}
geometry: {location: _.Q, viewport: _.R}
html_attributions: []
icon: "https://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png"
id: "5ffe179c6015e35e796ff6f3a35ff5149efd396d"
name: "Carrefour market"
opening_hours:
open_now: false
__proto__: Object
photos: Array(1)
0: {height: 4128, html_attributions: Array(1), width: 2322, getUrl: ƒ}
length: 1
__proto__: Array(0)
place_id: "ChIJx3_zP3I8w0cR5i6Q99tFeCU"
plus_code: {compound_code: "V84J+X6 Harelbeke, Belgium", global_code: "9F25V84J+X6"}
rating: 4.4
reference: "ChIJx3_zP3I8w0cR5i6Q99tFeCU"
scope: "GOOGLE"
types: (7) ["supermarket", "bakery", "grocery_or_supermarket", "store", "point_of_interest", "food", "establishment"]
user_ratings_total: 22
vicinity: "Mainstreet 137, NY"
__proto__: Object

标签: javascriptapigoogle-maps-api-3maps

解决方案


附近搜索请求没有fields参数。

您引用的文档是针对地方详细信息请求的,这不是您在提供的代码中使用的。

使用和计费页面还指定:

附近搜索请求返回具有完整地点详细信息的地点列表(附近搜索请求不支持指定返回的字段)。


推荐阅读