使用 PlacesService 反转地点 ID

Reverse place id using PlacesService

我正在寻找一种方法来避免因

而被收费

我只需要将地点 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);
                }
            }
        }
    }
});

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

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

如你所见,这个界面有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"]
};

希望对您有所帮助!