简体   繁体   English

我如何通过 JavaScript 使用 Google 的 AutocompleteService 获取地址组件?

[英]How can i get address components using Google's AutocompleteService via JavaScript?

Google offers different ways to use the Place Autocomplete API. Two of these being the Autocomplete and AutocompleteService classes. Google 提供了不同的方式来使用 Place Autocomplete API。其中两种是 Autocomplete 和 AutocompleteService 类。 Whenever I use the Autocomplete class I get the address components without a problem.每当我使用自动完成 class 时,我都会毫无问题地获得地址组件。 However, whenever I use the AutocompleteService the address components are missing.但是,每当我使用 AutocompleteService 时,地址组件都会丢失。 I do get an address but not in the same way I do with Autocomplete (I can parse into city, street, state, etc.).我确实得到了一个地址,但与我使用自动完成的方式不同(我可以解析为城市、街道、state 等)。

Is there a way I can get the address components field using AutocompleteService WITHOUT making an additional call with the place ID?有没有一种方法可以使用 AutocompleteService 获取地址组件字段,而无需使用地点 ID 进行额外的调用?

Autocomplete example自动完成示例

function initAutocomplete() {

  autocomplete = new google.maps.places.Autocomplete(document.getElementById('search'), {
    componentRestrictions: { country: ["us", "ca"] },
    fields: ["address_components", "geometry"],
    types: ["address"],
  });

  autocomplete.addListener("place_changed", () => {
    const place = autocomplete.getPlace(); // Includes Address Components and Geometry (Lat and Lng)
  });
}

AutocompleteService example自动完成服务示例

function initAutocompleteService() {
    const service = new google.maps.places.AutocompleteService();

    service.getPlacePredictions({
    input: "1600 Amphitheatre",
    componentRestrictions: {
        country: 'us'
    },
    types: ['address']
  }, (predictions, status) => {
    console.log(predictions); // It shows the address a single string but missing address components
  });
}

Is there a way I can get the address components field using AutocompleteService WITHOUT making an additional call with the place ID?有没有一种方法可以使用 AutocompleteService 获取地址组件字段,而无需使用地点 ID 进行额外的调用?

Short answer: No, you can't.简短的回答:不,你不能。

If you are worried about billing, you should use Session tokens .如果您担心计费,您应该使用Session 代币

As per the documentation:根据文档:

AutocompleteService.getPlacePredictions() uses session tokens to group together autocomplete requests for billing purposes. AutocompleteService.getPlacePredictions()使用 session 令牌将自动完成请求组合在一起以用于计费目的。

The interesting part:有趣的部分:

You can use the same session token to make a single Place Details request on the place that results from a call to AutocompleteService.getPlacePredictions() .您可以使用相同的 session 令牌对通过调用AutocompleteService.getPlacePredictions()产生的地点发出单个地点详细信息请求。 In this case, the autocomplete request is combined with the Place Details request, and the call is charged as a regular Place Details request.在这种情况下,自动完成请求与地点详情请求相结合,并且该呼叫作为常规地点详情请求收费。 There is no charge for the autocomplete request.自动完成请求是免费的。

Google provides an example on how to create the Session token: Google 提供了一个关于如何创建 Session 令牌的示例:

// Create a new session token.
var sessionToken = new google.maps.places.AutocompleteSessionToken();

// Pass the token to the autocomplete service.
var autocompleteService = new google.maps.places.AutocompleteService();
autocompleteService.getPlacePredictions({
  input: 'pizza near Syd',
  sessionToken: sessionToken
},
displaySuggestions);

When user selects a place, you can reuse that same session token to retrieve the place details with the fields you are interested in, in order to get billed only for the place details request (as stated above).当用户选择一个地点时,您可以重复使用相同的 session 令牌来检索包含您感兴趣的字段的地点详细信息,以便仅针对地点详细信息请求进行计费(如上所述)。

You might also want to read Cost optimization best practices .您可能还想阅读成本优化最佳实践

Programmatic implementation程序化实施

Use a session token with your Place Autocomplete requests.在您的地点自动完成请求中使用 session 令牌。 When requesting Place Details about the selected prediction, include the following parameters:请求有关所选预测的地点详细信息时,请包含以下参数:

  1. The place ID from the Place Autocomplete response来自地点自动完成响应的地点 ID
  2. The session token used in the Place Autocomplete request地点自动完成请求中使用的 session 令牌
  3. The fields parameter specifying the place data fields you need fields 参数指定你需要的地方数据字段

As you can see, the PlaceDetailsRequest interface takes an optional sessionToken parameter.如您所见, PlaceDetailsRequest 接口采用可选的sessionToken参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用JavaScript获取客户的国家/地区IP地址? - How to get client's country address Ip address using JavaScript? 如何通过单击时通过 Javascript 将其代码插入头部来激活 Google 标签管理器? - How can I activate Google Tag Manager by inserting it's code in the head via Javascript upon a click? 如何使用Facebook Javascript SDK获取人员地址? - How to get person's address using Facebook Javascript SDK? Google的AutocompleteService和Autocomplete会使用相同的查询返回不同的结果 - Google's AutocompleteService and Autocomplete returning different results with same query Google maps API V3 - 无法对AutocompleteService预测进行地理编码 - Google maps API V3 - Can't geocode AutocompleteService predictions 如何通过Java或JavaScript获取IP地址? - How to get IP address via java or javascript? 使用 AutocompleteService() 将 Google 地图位置限制在特定国家/地区 - Restricting Google Maps Places to Specific Countries using AutocompleteService() Google AutocompleteService getPlacePredictions 不起作用 - Google AutocompleteService getPlacePredictions is not working 我可以使用javascript和google-analytics获得有关用户地理位置的信息吗? - Can I get information about user's geolocation using javascript and google-analytics? 我怎样才能在javascript中在这个对象中获得一种类型的地址? - how can i get one type of address in this object in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM