首页 > 解决方案 > 使用 PlacesService 反转地点 ID

问题描述

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

我只需要将地点 id 反转为其formatted_address。关于我需要在下面的代码中进行哪些更改的任何想法?

相关问题,虽然我没有使用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);
                }
            }
        }
    }
});

标签: javascriptgoogle-mapsgoogle-places-api

解决方案


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

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

如您所见,此接口具有fields属性

fields - 要包含在详细信息响应中的字段。有关字段列表,请参阅 PlaceResult。可以使用点路径指定嵌套字段(例如,“geometry.location”)。

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

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

我希望这有帮助!


推荐阅读