【问题标题】:Reverse place id using PlacesService使用 PlacesService 反转地点 ID
【发布时间】:2019-12-10 23:26:54
【问题描述】:

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

  • Places 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);
                }
            }
        }
    }
});

【问题讨论】:

    标签: javascript google-maps google-places-api


    【解决方案1】:

    为了避免使用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"]
    };
    

    我希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-12
      • 1970-01-01
      • 1970-01-01
      • 2020-10-22
      • 1970-01-01
      • 2017-02-02
      • 1970-01-01
      相关资源
      最近更新 更多