Skip to content

Commit defe0b0

Browse files
committed
feat(package): integration of the new german address interface
1 parent 83a5ab4 commit defe0b0

File tree

8 files changed

+88
-15
lines changed

8 files changed

+88
-15
lines changed

projects/angular-material-extensions/google-maps-autocomplete/src/lib/directives/mat-google-maps-autocomplete.directive.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import {MatGoogleMapsAutocompleteDirective} from './mat-google-maps-autocomplete
22
import {async, TestBed} from '@angular/core/testing';
33
import {Component, DebugElement, ElementRef, NgZone} from '@angular/core';
44
import {MapsAPILoader} from '@agm/core';
5-
import {environment} from '../../../demo/src/environments/environment';
65
import {MockNgZone} from '../testing/mock-ng-zone';
6+
import {environment} from '../../../../../../src/environments/environment';
77

88
@Component({
99
template: `<input type="text">`

projects/angular-material-extensions/google-maps-autocomplete/src/lib/directives/mat-google-maps-autocomplete.directive.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {MatValidateAddressDirective} from '../directives/address-validator/mat-a
44
import {MapsAPILoader} from '@agm/core';
55
import {Location} from '../interfaces/location.interface';
66
import {isPlatformBrowser} from '@angular/common';
7+
import {GermanAddress} from '../interfaces/germand.address.interface';
78
import PlaceResult = google.maps.places.PlaceResult;
89
import AutocompleteOptions = google.maps.places.AutocompleteOptions;
910

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

44+
@Output()
45+
onGermanAddressMapped: EventEmitter<GermanAddress> = new EventEmitter<GermanAddress>();
46+
4347
@Output()
4448
onLocationSelected: EventEmitter<Location> = new EventEmitter<Location>();
4549

@@ -90,6 +94,57 @@ export class MatGoogleMapsAutocompleteDirective implements OnInit {
9094
// get the place result
9195
const place: PlaceResult = autocomplete.getPlace();
9296

97+
const germanAddress: GermanAddress = {
98+
gmID: place.id,
99+
icon: place.icon,
100+
url: place.url,
101+
placeID: place.place_id,
102+
displayAddress: place.formatted_address,
103+
name: place.name,
104+
vicinity: place.vicinity,
105+
locality: {},
106+
state: {},
107+
country: {},
108+
geoLocation: {latitude: -1, longitude: -1},
109+
};
110+
111+
if (place.geometry && place.geometry.location) {
112+
germanAddress.geoLocation.latitude = place.geometry.location.lat();
113+
germanAddress.geoLocation.longitude = place.geometry.location.lng();
114+
}
115+
116+
place.address_components.forEach(value => {
117+
if (value.types.indexOf('street_number') > -1) {
118+
germanAddress.streetNumber = Number(value.short_name);
119+
}
120+
if (value.types.indexOf('route') > -1) {
121+
germanAddress.streetName = value.long_name;
122+
}
123+
if (value.types.indexOf('postal_code') > -1) {
124+
germanAddress.postalCode = Number(value.short_name);
125+
}
126+
if (value.types.indexOf('sublocality') > -1) {
127+
germanAddress.sublocality = value.long_name;
128+
}
129+
if (value.types.indexOf('locality') > -1) {
130+
germanAddress.locality.long = value.long_name;
131+
germanAddress.locality.short = value.short_name;
132+
}
133+
if (value.types.indexOf('administrative_area_level_1') > -1) {
134+
germanAddress.state.long = value.long_name;
135+
germanAddress.state.short = value.short_name;
136+
}
137+
if (value.types.indexOf('country') > -1) {
138+
germanAddress.country.long = value.long_name;
139+
germanAddress.country.short = value.short_name;
140+
}
141+
if (value.types.indexOf('administrative_area_level_3') > -1) {
142+
germanAddress.locality.short = value.short_name;
143+
}
144+
});
145+
146+
this.onGermanAddressMapped.emit(germanAddress);
147+
93148
if (!place.place_id || place.geometry === undefined || place.geometry === null) {
94149
// place result is not valid
95150
return;

projects/angular-material-extensions/google-maps-autocomplete/src/lib/interfaces/germand.address.interface.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,24 @@ export interface GermanAddress {
1111
placeID: string;
1212
name?: string;
1313
icon?: string;
14-
displayAddress: string;
15-
postalCode: number;
16-
streetNumber: number;
17-
streetName: string;
18-
sublocality: string;
19-
locality: string;
20-
state: string;
21-
country: string;
14+
displayAddress?: string;
15+
postalCode?: number;
16+
streetNumber?: number;
17+
streetName?: string;
18+
sublocality?: string;
19+
locality?: {
20+
short?: string;
21+
long?: string;
22+
};
23+
state?: {
24+
short?: string;
25+
long?: string;
26+
};
27+
country?: {
28+
short?: string;
29+
long?: string;
30+
};
2231
vicinity?: string;
2332
url?: string;
24-
location?: Location;
33+
geoLocation?: Location;
2534
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './germand.address.interface';
2+
export * from './location.interface';

projects/angular-material-extensions/google-maps-autocomplete/src/lib/mat-google-maps-autocomplete.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {NgModule} from '@angular/core';
22
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
33
import {MatInputModule} from '@angular/material';
4-
import {MatGoogleMapsAutocompleteDirective} from './directives/mat-google-maps-autocomplete.directive';
54
import {MatGoogleMapsAutocompleteComponent} from './component/mat-google-maps-autocomplete.component';
5+
import {MatGoogleMapsAutocompleteDirective} from './directives/mat-google-maps-autocomplete.directive';
66
import {MatValidateAddressDirective} from './directives/address-validator/mat-address-validator.directive';
77
import {CommonModule} from '@angular/common';
88

projects/angular-material-extensions/google-maps-autocomplete/src/public-api.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
* Public API Surface of google-maps-autocomplete
33
*/
44

5+
export * from './lib/interfaces';
56
export * from './lib/component/mat-google-maps-autocomplete.component';
67
export * from './lib/directives/mat-google-maps-autocomplete.directive';
78
export * from './lib/directives/address-validator/mat-address-validator.directive';
8-
export * from './lib/interfaces/location.interface';
99
export * from './lib/mat-google-maps-autocomplete.module';
10+

src/app/app.component.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ <h2>Usage</h2>
468468
[country]="config.country"
469469
(onAutocompleteSelected)="onAutocompleteSelected($event)"
470470
(onLocationSelected)="onLocationSelected($event)"
471+
(onGermanAddressMapped)="onGermanAddressMapped($event)"
471472
required>
472473
<mat-error *ngIf="matGoogleMapsAutocomplete.addressSearchControl.hasError('required')">
473474
{{config.requiredErrorText}}
@@ -489,7 +490,8 @@ <h2>Usage</h2>
489490
[types]="['bank']"
490491
[appearance]="config.appearance"
491492
(onAutocompleteSelected)="onAutocompleteSelected($event)"
492-
(onLocationSelected)="onLocationSelected($event)">
493+
(onLocationSelected)="onLocationSelected($event)"
494+
(onGermanAddressMapped)="onGermanAddressMapped($event)">
493495
</mat-google-maps-autocomplete>
494496
</div>
495497

src/app/app.component.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {Component} from '@angular/core';
2-
import {Appearance, Location} from '@angular-material-extensions/google-maps-autocomplete';
3-
import PlaceResult = google.maps.places.PlaceResult;
2+
import {Appearance, GermanAddress, Location} from '@angular-material-extensions/google-maps-autocomplete';
43
import {Angulartics2GoogleAnalytics} from 'angulartics2/ga';
4+
import PlaceResult = google.maps.places.PlaceResult;
55

66
@Component({
77
selector: 'app-root',
@@ -47,4 +47,8 @@ export class AppComponent {
4747
this.showAsDirective = !this.showAsDirective;
4848
this.showAsComponent = !this.showAsDirective;
4949
}
50+
51+
onGermanAddressMapped($event: GermanAddress) {
52+
console.log('onGermanAddressMapped', $event);
53+
}
5054
}

0 commit comments

Comments
 (0)