Skip to content

Commit

Permalink
feat(package): integration of the new german address interface
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyNahas committed Dec 23, 2019
1 parent 83a5ab4 commit defe0b0
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {MatGoogleMapsAutocompleteDirective} from './mat-google-maps-autocomplete
import {async, TestBed} from '@angular/core/testing';
import {Component, DebugElement, ElementRef, NgZone} from '@angular/core';
import {MapsAPILoader} from '@agm/core';
import {environment} from '../../../demo/src/environments/environment';
import {MockNgZone} from '../testing/mock-ng-zone';
import {environment} from '../../../../../../src/environments/environment';

@Component({
template: `<input type="text">`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {MatValidateAddressDirective} from '../directives/address-validator/mat-a
import {MapsAPILoader} from '@agm/core';
import {Location} from '../interfaces/location.interface';
import {isPlatformBrowser} from '@angular/common';
import {GermanAddress} from '../interfaces/germand.address.interface';
import PlaceResult = google.maps.places.PlaceResult;
import AutocompleteOptions = google.maps.places.AutocompleteOptions;

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

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

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

Expand Down Expand Up @@ -90,6 +94,57 @@ export class MatGoogleMapsAutocompleteDirective implements OnInit {
// 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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,24 @@ export interface GermanAddress {
placeID: string;
name?: string;
icon?: string;
displayAddress: string;
postalCode: number;
streetNumber: number;
streetName: string;
sublocality: string;
locality: string;
state: string;
country: string;
displayAddress?: string;
postalCode?: number;
streetNumber?: number;
streetName?: string;
sublocality?: string;
locality?: {
short?: string;
long?: string;
};
state?: {
short?: string;
long?: string;
};
country?: {
short?: string;
long?: string;
};
vicinity?: string;
url?: string;
location?: Location;
geoLocation?: Location;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './germand.address.interface';
export * from './location.interface';
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {NgModule} from '@angular/core';
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
import {MatInputModule} from '@angular/material';
import {MatGoogleMapsAutocompleteDirective} from './directives/mat-google-maps-autocomplete.directive';
import {MatGoogleMapsAutocompleteComponent} from './component/mat-google-maps-autocomplete.component';
import {MatGoogleMapsAutocompleteDirective} from './directives/mat-google-maps-autocomplete.directive';
import {MatValidateAddressDirective} from './directives/address-validator/mat-address-validator.directive';
import {CommonModule} from '@angular/common';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
* Public API Surface of google-maps-autocomplete
*/

export * from './lib/interfaces';
export * from './lib/component/mat-google-maps-autocomplete.component';
export * from './lib/directives/mat-google-maps-autocomplete.directive';
export * from './lib/directives/address-validator/mat-address-validator.directive';
export * from './lib/interfaces/location.interface';
export * from './lib/mat-google-maps-autocomplete.module';

4 changes: 3 additions & 1 deletion src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ <h2>Usage</h2>
[country]="config.country"
(onAutocompleteSelected)="onAutocompleteSelected($event)"
(onLocationSelected)="onLocationSelected($event)"
(onGermanAddressMapped)="onGermanAddressMapped($event)"
required>
<mat-error *ngIf="matGoogleMapsAutocomplete.addressSearchControl.hasError('required')">
{{config.requiredErrorText}}
Expand All @@ -489,7 +490,8 @@ <h2>Usage</h2>
[types]="['bank']"
[appearance]="config.appearance"
(onAutocompleteSelected)="onAutocompleteSelected($event)"
(onLocationSelected)="onLocationSelected($event)">
(onLocationSelected)="onLocationSelected($event)"
(onGermanAddressMapped)="onGermanAddressMapped($event)">
</mat-google-maps-autocomplete>
</div>

Expand Down
8 changes: 6 additions & 2 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Component} from '@angular/core';
import {Appearance, Location} from '@angular-material-extensions/google-maps-autocomplete';
import PlaceResult = google.maps.places.PlaceResult;
import {Appearance, GermanAddress, Location} from '@angular-material-extensions/google-maps-autocomplete';
import {Angulartics2GoogleAnalytics} from 'angulartics2/ga';
import PlaceResult = google.maps.places.PlaceResult;

@Component({
selector: 'app-root',
Expand Down Expand Up @@ -47,4 +47,8 @@ export class AppComponent {
this.showAsDirective = !this.showAsDirective;
this.showAsComponent = !this.showAsDirective;
}

onGermanAddressMapped($event: GermanAddress) {
console.log('onGermanAddressMapped', $event);
}
}

0 comments on commit defe0b0

Please sign in to comment.