Skip to content

Commit

Permalink
feat(package): new event emitter onGermanAddressMapped
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyNahas committed Dec 23, 2019
1 parent defe0b0 commit 3ae431f
Showing 1 changed file with 60 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {FormControl, Validators} from '@angular/forms';
import {MapsAPILoader} from '@agm/core';
import {MatValidateAddressDirective} from '../directives/address-validator/mat-address-validator.directive';
import {Location} from '../interfaces/location.interface';
import {GermanAddress} from '../interfaces';
import PlaceResult = google.maps.places.PlaceResult;
import AutocompleteOptions = google.maps.places.AutocompleteOptions;

Expand Down Expand Up @@ -67,6 +68,9 @@ export class MatGoogleMapsAutocompleteComponent implements OnInit {
@Output()
onAutocompleteSelected: EventEmitter<PlaceResult> = new EventEmitter<PlaceResult>();

@Output()
onGermanAddressMapped: EventEmitter<GermanAddress> = new EventEmitter<GermanAddress>();

@Output()
onLocationSelected: EventEmitter<Location> = new EventEmitter<Location>();

Expand All @@ -78,8 +82,8 @@ export class MatGoogleMapsAutocompleteComponent implements OnInit {
this.addressValidator.validate()])
);

constructor(private _mapsAPILoader: MapsAPILoader,
private _ngZone: NgZone) {
constructor(private mapsAPILoader: MapsAPILoader,
private ngZone: NgZone) {
}

ngOnInit(): void {
Expand All @@ -104,15 +108,66 @@ export class MatGoogleMapsAutocompleteComponent implements OnInit {
}

public initGoogleMapsAutocomplete() {
this._mapsAPILoader
this.mapsAPILoader
.load()
.then(() => {
const autocomplete = new google.maps.places.Autocomplete(this.searchElementRef.nativeElement, this.autoCompleteOptions);
autocomplete.addListener('place_changed', () => {
this._ngZone.run(() => {
this.ngZone.run(() => {
// get the place result
const place: PlaceResult = autocomplete.getPlace();

const germanAddress: GermanAddress = {
gmID: place.id,
icon: place.icon,
url: place.url,
placeID: place.place_id,
displayAddress: place.formatted_address,
name: place.name,
vicinity: place.vicinity,
locality: {},
state: {},
country: {},
geoLocation: {latitude: -1, longitude: -1},
};

if (place.geometry && place.geometry.location) {
germanAddress.geoLocation.latitude = place.geometry.location.lat();
germanAddress.geoLocation.longitude = place.geometry.location.lng();
}

place.address_components.forEach(value => {
if (value.types.indexOf('street_number') > -1) {
germanAddress.streetNumber = Number(value.short_name);
}
if (value.types.indexOf('route') > -1) {
germanAddress.streetName = value.long_name;
}
if (value.types.indexOf('postal_code') > -1) {
germanAddress.postalCode = Number(value.short_name);
}
if (value.types.indexOf('sublocality') > -1) {
germanAddress.sublocality = value.long_name;
}
if (value.types.indexOf('locality') > -1) {
germanAddress.locality.long = value.long_name;
germanAddress.locality.short = value.short_name;
}
if (value.types.indexOf('administrative_area_level_1') > -1) {
germanAddress.state.long = value.long_name;
germanAddress.state.short = value.short_name;
}
if (value.types.indexOf('country') > -1) {
germanAddress.country.long = value.long_name;
germanAddress.country.short = value.short_name;
}
if (value.types.indexOf('administrative_area_level_3') > -1) {
germanAddress.locality.short = value.short_name;
}
});

this.onGermanAddressMapped.emit(germanAddress);

if (!place.place_id || place.geometry === undefined || place.geometry === null) {
// place result is not valid
return;
Expand All @@ -127,7 +182,7 @@ export class MatGoogleMapsAutocompleteComponent implements OnInit {
{
latitude: place.geometry.location.lat(),
longitude: place.geometry.location.lng()
})
});
});
});
})
Expand Down

0 comments on commit 3ae431f

Please sign in to comment.