指定起始位置对于谷歌地方自动填充有角

问题描述 投票:5回答:1

我使用的是棱角分明谷歌商家信息自动填充API由AGM提供。它的伟大工程,但我有麻烦的手动设置我的起始位置。也就是说,如果我坐在我在洛杉矶的电脑,我想告诉API,好像我是在波士顿返回结果的能力。我相信这是可能在谷歌的最后,基于their documentation,但我还没有想出如何我使用的角度成分的范围内做到这一点。

任何人有关于如何得到这个工作小费了吗?下面是我的组件的相关部分。谢谢!

  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;
        });
      });
    });
  }
angular angular-google-maps
1个回答
1
投票

显然,你已经使用的代码从这个例子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密钥,因为我的名额似乎被超过。

UPDATE

您还需要设置内要返回的地方结果radius

© www.soinside.com 2019 - 2024. All rights reserved.