谷歌角度地图自动完成特定城市的地址

时间:2020-11-04 19:56:00

标签: angular google-maps google-maps-autocomplete

我正在使用Google地图自动完成功能来搜索地址,用户首先选择国家/地区,然后基于这些输入,我需要将搜索限制在该用户选择的特定城市。

  ngAfterViewInit() {
    this.getPlaceAutocomplete();
  }


  private getPlaceAutocomplete() {
    this.autocomplete = new google.maps.places.Autocomplete(this.addresstext.nativeElement,
        {
            componentRestrictions: { country: 'RS' },
            types: ['address']  // 'establishment' / 'address' / 'geocode'
        });
    google.maps.event.addListener(this.autocomplete, 'place_changed', () => {
        const place = this.autocomplete.getPlace();
        this.locationForm.get('adresa').setValue(place.name);
        this.lat = place.geometry.location.lat();
        this.lng = place.geometry.location.lng();
        this.locationForm.get('coordinates').setValue([this.lng, this.lat]);
    });
  }

0 个答案:

没有答案