javascript - 使用 PlacesService 反转地点 ID

标签 javascript google-maps google-places-api

我正在寻找一种避免被收费的方法

  • 地点 API 大气数据
  • Places API 联系数据

我只需要将地点 ID 反转为其 formatted_address 。知道我需要在下面的代码中进行哪些更改吗?

Related question ,虽然我没有使用 Autocomplete 而是使用 PlacesService

<小时/>
$('.company_location_id').each(function(i, obj) {
    if ($(this).length > 0) {
        if ($(this).val()) {
            var request = {
                placeId: $(this).val()
            };

            service = new google.maps.places.PlacesService(document.getElementsByClassName($(this).attr('class'))[0]);
            service.getDetails(request, callback);

            var new_text = $(this)

            function callback(place, status) {
                if (status == google.maps.places.PlacesServiceStatus.OK) {
                    $(new_text).next().text(place.formatted_address);
                }
            }
        }
    }
});

最佳答案

为了避免使用 PlacesService 对大气数据和联系数据产生费用,您应该在作为传递的 PlaceDetailsRequest 对象中指定要检索的字段列表getDetails() 函数中的第一个参数。

查看 PlaceDetailsRequest 接口(interface)的文档: https://developers.google.com/maps/documentation/javascript/reference/places-service#PlaceDetailsRequest

如您所见,该接口(interface)具有 fields 属性

fields - Fields to be included in the details response. For a list of fields see PlaceResult. Nested fields can be specified with dot-paths (for example, "geometry.location").

因此,您应该在代码中将请求定义为

var request = {
    placeId: $(this).val(),
    fields: ["formatted_address"]
};

希望这会有所帮助!

关于javascript - 使用 PlacesService 反转地点 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57324707/

相关文章:

javascript - 如何将javascript var注入(inject)css

javascript - Google map API place.url 返回 "undefined"?

javascript - 谷歌地图 : Simple app not working on IE

google-maps - 在黑莓应用程序中使用谷歌地图

jquery - 为什么 Google 地理编码有时只返回邮政编码?

使用Google Places的angularjs UI引导模态

google-maps - 如何通过Google Places API获取业务 "description"?

javascript - toFixed 不是一个函数吗?

javascript - 将数组转换为对象并将值粘贴到不同的列中

javascript - 间歇性 browserify 捆绑失败(可能是 brfs)