我正在寻找一种避免被收费的方法
- 地点 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/