角度谷歌地图(AGM)自动完成

时间:2017-12-19 11:52:37

标签: angularjs angular google-maps google-maps-api-3 angular-google-maps

我使用有角度的谷歌地图(AGM)来查找地址。现在我希望当一个人选择并解决它时填充输入。我在哪里可以找到像street,str这样的单个地址元素名称。数字,邮政编码等将数据绑定到输入字段?

4 个答案:

答案 0 :(得分:3)

我们可以使用Google Maps api找到一些详细信息,请按照以下步骤操作。

第1步:将MapsAPILoader导入您的组件。

import { MapsAPILoader } from '@agm/core';
@ViewChild('search') searchElementRef: ElementRef;

第2步:实施findAdress()方法,并在您的ngAfterViewInit()方法中调用它。

findAdress(){
 this.mapsAPILoader.load().then(() => {
      let autocomplete = new google.maps.places.Autocomplete(this.searchElementRef.nativeElement);
      autocomplete.addListener("place_changed", () => {
        this.ngZone.run(() => {
          // some details
          let place: google.maps.places.PlaceResult = autocomplete.getPlace();
          this.address = place.formatted_address;
          this.web_site = place.website;
          this.name = place.name;
          this.zip_code = place.address_components[place.address_components.length - 1].long_name;
          //set latitude, longitude and zoom
          this.latitude = place.geometry.location.lat();
          this.longitude = place.geometry.location.lng();
          this.zoom = 12;
        });
      });
    });
}

第3步:在html文件中进行一些更改。

  <input #search autocorrect="off" autocapitalize="off" spellcheck="off" type="text" class="form-control">

第4步:别忘了在AgmCoreModule配置中添加位置信息库。

    import { AgmCoreModule } from '@agm/core';


   imports: [
      AgmCoreModule.forRoot({
          apiKey: 'your_key',
          libraries: ["places"]
        })
]

答案 1 :(得分:1)

你会在这里找到所需的一切 https://developers.google.com/maps/

我没有在Angular上使用AGM我使用常规API  Angular AGM对我不起作用,但常规API成功运作。

答案 2 :(得分:0)

我在标签中看到了角度和角度。对于angular2 +,您应该能够对本教程有用http://brianflove.com/2016/10/18/angular-2-google-maps-places-autocomplete/

对于angularjs,这是一个很好的参考填充https://jsfiddle.net/upsidown/1svw299r/。看看他们所做的部分google.maps.event.addListener。这是魔术的一部分。

此外,https://developers.google.com/maps/documentation/geocoding/intro#Types是地点对象返回的内容。

答案 3 :(得分:0)

尝试一下:

<ion-searchbar #searchbar [hidden]="!isSearchbarOpened" [showCancelButton]="true" (search)="onSearch($event)"
  (ionCancel)="isSearchbarOpened = false" (ionBlur)="isSearchbarOpened = false" autocorrect="off" autocapitalize="off"
  spellcheck="off" placeholder="Buscar por endereço">
</ion-searchbar>



@ViewChild('searchbar', { read: ElementRef }) public searchbarElement: ElementRef;



...



// place_changed and geocoding 
this.mapsAPILoader.load().then(() => {

  const searchInput = this.searchbarElement.nativeElement.querySelector('.searchbar-input');

  const autocomplete = new google.maps.places.Autocomplete(searchInput, {
    // types: ['address']
  });

  autocomplete.addListener('place_changed', () => {
    this.ngZone.run(() => {
      const place: google.maps.places.PlaceResult == autocomplete.getPlace();

      if (place.geometry === undefined || place.geometry === null) {
        console.log('place.geometry undefined');
        return;
      }

      this.zoom = 15;
      this.getAddress(place);
      this.isSearchbarOpened = false;
    });
  });
});

我正在使用离子4中的ion-searchbar

getAddress(place: Object) {
    this.formatted_address = place['formatted_address'];
    const location = place['geometry']['location'];
    // this.lat = location.lat();
    // this.lng = location.lng();
    console.log(place);

    this.lat = location.lat();
    this.lng = location.lng();

    console.log('formatted_address:', this.formatted_address);
    console.log('Address Object', place);
    this.geocode();

}


geocode() {
  this.placesService.newPlace.lat = this.lat;
  this.placesService.newPlace.lng = this.lng;

  const geocoder = new google.maps.Geocoder();

  const latlng = new google.maps.LatLng(this.lat, this.lng);

  geocoder.geocode({ 'location': latlng }, (results, status) => {
  if (status === google.maps.GeocoderStatus.OK) {

    if (results[0]) {
      // console.log('Location: ' + results[0].formatted_address);
      let houseNumber = '';
      let streetName = '';
      let neighborhood = '';
      let cityName = '';
      let stateName = '';
      let countryName = '';
      let shortAddress = '';
      let zipCode = '';

      // console.log(results[0].address_components.length);
      console.log(results[0].address_components);

      if (results[0].address_components.length === 3) {

        if (results[0].address_components[0].long_name !== null) {
          cityName = results[0].address_components[0].long_name;
        }

        if (results[0].address_components[1].short_name) {
          stateName = results[0].address_components[1].short_name;
        }

        if (results[0].address_components[2].short_name !== null) {
          countryName = results[0].address_components[2].short_name;
        }
      }

      if (results[0].address_components.length === 4) {

        if (results[0].address_components[0].long_name !== null) {
          streetName = results[0].address_components[0].long_name;
        }

        if (results[0].address_components[1].long_name !== null) {
          cityName = results[0].address_components[1].long_name;
        }

        if (results[0].address_components[2].short_name) {
          stateName = results[0].address_components[2].short_name;
        }

        if (results[0].address_components[3].short_name !== null) {
          countryName = results[0].address_components[3].short_name;
        }
      }

      if (results[0].address_components.length === 5) {

        if (results[0].address_components[0].long_name !== null) {
          streetName = results[0].address_components[0].long_name;
        }

        if (results[0].address_components[1].long_name !== null) {
          cityName = results[0].address_components[1].long_name;
        }

        if (results[0].address_components[2].short_name !== null) {
          stateName = results[0].address_components[2].short_name;
        }

        if (results[0].address_components[3].short_name !== null) {
          countryName = results[0].address_components[3].short_name;
        }

        if (results[0].address_components[4].short_name !== null) {
          zipCode = results[0].address_components[4].short_name;
        }

      }

      if (results[0].address_components.length === 6) {

        if (results[0].address_components[0].long_name !== null) {
          houseNumber = results[0].address_components[0].long_name;
        }

        if (results[0].address_components[1].long_name !== null) {
          streetName = results[0].address_components[1].long_name;
        }

        if (results[0].address_components[2].long_name !== null) {
          cityName = results[0].address_components[2].long_name;
        }

        if (results[0].address_components[3].short_name) {
          stateName = results[0].address_components[3].short_name;
        }

        if (results[0].address_components[4].short_name !== null) {
          countryName = results[0].address_components[4].short_name;
        }
      }

      if (results[0].address_components.length === 7) {

        if (results[0].address_components[0].long_name !== null) {
          houseNumber = results[0].address_components[0].long_name;
        }

        if (results[0].address_components[1].long_name !== null) {
          streetName = results[0].address_components[1].long_name;
        }

        if (results[0].address_components[2].long_name !== null) {
          neighborhood = results[0].address_components[2].long_name;
        }

        if (results[0].address_components[3].long_name !== null) {
          cityName = results[0].address_components[3].long_name;
        }

        if (results[0].address_components[4].short_name) {
          stateName = results[0].address_components[4].short_name;
        }

        if (results[0].address_components[5].short_name !== null) {
          countryName = results[0].address_components[5].short_name;
        }
      }




      if (cityName !== '') {
        shortAddress = cityName;
      }
      if (cityName !== '' && stateName !== '') {
        shortAddress += '-' + stateName;
      }
      if (shortAddress !== '' && countryName !== '') {
        shortAddress += ', ' + countryName;
      }
      if (cityName === '' && stateName === '' && countryName === '') {
        shortAddress = '???';
      }

      // console.log('shortAddress:', shortAddress);


      // Set addresses
      this.shortAddress = shortAddress;
      this.fullAddress = results[0].formatted_address;

      const newPlace = {
        lat: this.lat,
        lng: this.lng,
        shortAddress: this.shortAddress,
        fullAddress: this.fullAddress
      };

      this.events.publish('location:changed', newPlace);
    }
  }
});