为 Google 个地点指定起始位置自动完成 Angular

Specifying Starting Location For Google Places Autocomplete with Angular

我正在使用 Google 地点自动完成 API 和 AGM. It works great, but I'm having trouble setting my starting location manually. Meaning, if I'm sitting at my computer in Los Angeles, I would like the ability to tell the API to return results as if I were in Boston. I believe this is possible on Google's end, based on their documentation 提供的 Angular,但我还没有弄清楚如何在Angular 我正在使用的组件。

有人知道如何让它工作吗?下面是我的组件的相关部分。谢谢!

  setGoogleMapsAutocomplete() {
    this.mapsAPILoader.load().then(() => {
      let autocomplete = new google.maps.places.Autocomplete(this.searchElementRef.nativeElement, {
        types: ["address"]
      });
      autocomplete.addListener("place_changed", () => {
        this.ngZone.run(() => {
          let place = autocomplete.getPlace();

          this.address = '';

          for (var i = 0; i < place.address_components.length; i++)
          {
            var addressType = place.address_components[i].types[0];

            if (this.hasOwnProperty(addressType)) {
              let nameLength = addressType == 'administrative_area_level_1' ? 'short_name' : 'long_name';

              this[addressType] = place.address_components[i][nameLength];
            }
          }
          this.error = false;
        });
      });
    });
  }

显然您一直在使用此示例中的代码 https://brianflove.com/2016/10/18/angular-2-google-maps-places-autocomplete/

您是否尝试添加 location 键并将 lat,lon 作为值传递给作为第二个参数传递给 google.maps.places.Autocomplete 的对象。所以它应该是这样的:

    let autocomplete = new google.maps.places.Autocomplete(this.searchElementRef.nativeElement, {
        types: ["address"],
        radius: 50000,
        location: `${this.latitude}, ${this.longitude}`
    });

我已经为您创建了一个 stackblitz 沙箱 https://stackblitz.com/edit/angular-google-maps-demo-qwrngk

随便玩玩,但不要忘记更改 API 密钥,因为我的配额似乎已超出。

更新

您还需要设置要在其中 return 放置结果的 radius