使用Google Maps Places API获取place_id

时间:2019-10-02 10:06:45

标签: angular typescript google-maps

我正在研究Angular项目,并尝试获取地址的place_id,因此我可以将其与Maps Embed API一起使用,并在页面上显示结果。

现在,我通过发送具有给定地址的http请求来使用Maps Embed API: https://www.google.com/maps/embed/v1/place?key=API_KEY&zoom=18&maptype=satellite&q=Example%20Street%201%2C%209999%20Example%20Town%20Example%20Country

但是我的目标是根据用户输入地址的结果向用户显示消息。

例如,如果用户输入了一条找不到的街道,但输入的城市有效,则地图应指向该城市。如果街道和城市无效,则会弹出“找不到街道或城市!...”之类的消息。但是Maps Embed API的问题在于我没有得到回复,因此我可以进行验证。

所以我以为我会先执行place_id请求,如果再返回一次,我将使用Maps Embed API,否则向用户显示弹出消息。

我可以使用Places API和Maps JavaScript API来获取place_id。 这是代码:

    const request = {
    query: 'New York',
    fields: ['place_id']
    };

    this.map = new google.maps.Map(this.mapElement.nativeElement);

    this.service = new google.maps.places.PlacesService(this.map);
    this.service.findPlaceFromQuery(request, (results, status) => {
      if(status === google.maps.places.PlacesServiceStatus.OK) {
        this.placeId = results[0].place_id;
      }
    })

根据docs,对google.maps.Map()的使用进行计费,但是使用Places API来获取basic data是免费的。为了能够使用PlacesService,我需要传递一个Map对象。

所以我的问题是,是否有另一种方法可以获取place_id而又无需通过Maps JavaScript API进行计费?

0 个答案:

没有答案