1
1
import {
2
+ AfterViewInit ,
2
3
ChangeDetectorRef ,
3
4
Directive ,
4
5
ElementRef ,
@@ -8,14 +9,17 @@ import {
8
9
Inject ,
9
10
Input ,
10
11
NgZone ,
12
+ OnDestroy ,
11
13
OnInit ,
12
14
Output ,
13
- PLATFORM_ID
15
+ PLATFORM_ID ,
16
+ ViewChild
14
17
} from '@angular/core' ;
15
18
import { ControlValueAccessor , FormControl , NG_VALIDATORS , Validators } from '@angular/forms' ;
16
- import { MapsAPILoader } from '@agm/core' ;
17
19
import { GermanAddress , Location } from '../interfaces' ;
18
20
import { isPlatformBrowser } from '@angular/common' ;
21
+ import { ScriptLoaderService } from "../services/script-loader.service" ;
22
+ import { ApiKeyToken } from "../tokens" ;
19
23
import PlaceResult = google . maps . places . PlaceResult ;
20
24
import AutocompleteOptions = google . maps . places . AutocompleteOptions ;
21
25
@@ -30,7 +34,12 @@ import AutocompleteOptions = google.maps.places.AutocompleteOptions;
30
34
}
31
35
]
32
36
} )
33
- export class MatGoogleMapsAutocompleteDirective implements OnInit , ControlValueAccessor {
37
+ export class MatGoogleMapsAutocompleteDirective implements OnInit , AfterViewInit , OnDestroy , ControlValueAccessor {
38
+
39
+ @ViewChild ( 'inputField' )
40
+ inputField ! : ElementRef ;
41
+
42
+ autocomplete : google . maps . places . Autocomplete | undefined ;
34
43
35
44
@Input ( )
36
45
address : PlaceResult | string ;
@@ -86,31 +95,24 @@ export class MatGoogleMapsAutocompleteDirective implements OnInit, ControlValueA
86
95
} ;
87
96
88
97
constructor ( @Inject ( PLATFORM_ID ) public platformId : string ,
98
+ @Inject ( ApiKeyToken )
99
+ public apiKey : string ,
89
100
public elemRef : ElementRef ,
90
- public mapsAPILoader : MapsAPILoader ,
91
101
private cf : ChangeDetectorRef ,
102
+ private loaderService : ScriptLoaderService ,
92
103
private ngZone : NgZone ) {
93
104
}
94
105
95
- ngOnInit ( ) : void {
96
- if ( isPlatformBrowser ( this . platformId ) ) {
97
- const options : AutocompleteOptions = {
98
- // types: ['address'],
99
- // componentRestrictions: {country: this.country},
100
- placeIdOnly : this . placeIdOnly ,
101
- strictBounds : this . strictBounds ,
102
- // types: this.types,
103
- type : this . type
104
- } ;
106
+ ngOnDestroy ( ) : void {
107
+ throw new Error ( 'Method not implemented.' ) ;
108
+ }
105
109
106
- // tslint:disable-next-line:no-unused-expression
107
- this . country ? options . componentRestrictions = { country : this . country } : null ;
108
- // tslint:disable-next-line:no-unused-expression
109
- this . country ? options . types = this . types : null ;
110
+ ngAfterViewInit ( ) : void {
111
+ this . loadMap ( ) ;
112
+ }
110
113
111
- this . autoCompleteOptions = Object . assign ( this . autoCompleteOptions , options ) ;
112
- this . initGoogleMapsAutocomplete ( ) ;
113
- }
114
+ ngOnInit ( ) : void {
115
+ console . log ( "this.apiKey = " , this . apiKey )
114
116
}
115
117
116
118
validate ( fc : FormControl ) {
@@ -124,78 +126,73 @@ export class MatGoogleMapsAutocompleteDirective implements OnInit, ControlValueA
124
126
}
125
127
126
128
public initGoogleMapsAutocomplete ( ) {
127
- this . mapsAPILoader
128
- . load ( )
129
- . then ( ( ) => {
130
- const autocomplete = new google . maps . places . Autocomplete ( this . elemRef . nativeElement , this . autoCompleteOptions ) ;
131
- autocomplete . addListener ( 'place_changed' , ( ) => {
132
- this . ngZone . run ( ( ) => {
133
- // get the place result
134
- const place : PlaceResult = autocomplete . getPlace ( ) ;
135
-
136
- const germanAddress : GermanAddress = {
137
- gmID : place . id ,
138
- icon : place . icon ,
139
- url : place . url ,
140
- placeID : place . place_id ,
141
- displayAddress : place . formatted_address ,
142
- name : place . name ,
143
- vicinity : place . vicinity ,
144
- locality : { } ,
145
- state : { } ,
146
- country : { } ,
147
- geoLocation : { latitude : - 1 , longitude : - 1 } ,
148
- } ;
149
-
150
- if ( place . geometry && place . geometry . location ) {
151
- germanAddress . geoLocation . latitude = place . geometry . location . lat ( ) ;
152
- germanAddress . geoLocation . longitude = place . geometry . location . lng ( ) ;
153
- }
154
-
155
- place . address_components . forEach ( value => {
156
- if ( value . types . indexOf ( 'street_number' ) > - 1 ) {
157
- germanAddress . streetNumber = value . short_name ;
158
- }
159
- if ( value . types . indexOf ( 'route' ) > - 1 ) {
160
- germanAddress . streetName = value . long_name ;
161
- }
162
- if ( value . types . indexOf ( 'postal_code' ) > - 1 ) {
163
- germanAddress . postalCode = Number ( value . short_name ) ;
164
- }
165
- if ( value . types . indexOf ( 'sublocality' ) > - 1 ) {
166
- germanAddress . sublocality = value . long_name ;
167
- }
168
- if ( value . types . indexOf ( 'locality' ) > - 1 ) {
169
- germanAddress . locality . long = value . long_name ;
170
- germanAddress . locality . short = value . short_name ;
171
- }
172
- if ( value . types . indexOf ( 'administrative_area_level_1' ) > - 1 ) {
173
- germanAddress . state . long = value . long_name ;
174
- germanAddress . state . short = value . short_name ;
175
- }
176
- if ( value . types . indexOf ( 'country' ) > - 1 ) {
177
- germanAddress . country . long = value . long_name ;
178
- germanAddress . country . short = value . short_name ;
179
- }
180
- if ( value . types . indexOf ( 'administrative_area_level_3' ) > - 1 ) {
181
- germanAddress . locality . short = value . short_name ;
182
- }
183
- } ) ;
184
-
185
- this . onGermanAddressMapped . emit ( germanAddress ) ;
186
-
187
- this . value = place . formatted_address ;
188
- this . address = place . formatted_address ;
189
- this . onAutocompleteSelected . emit ( place ) ;
190
- this . onLocationSelected . emit (
191
- {
192
- latitude : place . geometry . location . lat ( ) ,
193
- longitude : place . geometry . location . lng ( )
194
- } ) ;
195
- } ) ;
129
+ const autocomplete = new google . maps . places . Autocomplete ( this . elemRef . nativeElement , this . autoCompleteOptions ) ;
130
+ autocomplete . addListener ( 'place_changed' , ( ) => {
131
+ this . ngZone . run ( ( ) => {
132
+ // get the place result
133
+ const place : PlaceResult = autocomplete . getPlace ( ) ;
134
+
135
+ const germanAddress : GermanAddress = {
136
+ gmID : place . id ,
137
+ icon : place . icon ,
138
+ url : place . url ,
139
+ placeID : place . place_id ,
140
+ displayAddress : place . formatted_address ,
141
+ name : place . name ,
142
+ vicinity : place . vicinity ,
143
+ locality : { } ,
144
+ state : { } ,
145
+ country : { } ,
146
+ geoLocation : { latitude : - 1 , longitude : - 1 } ,
147
+ } ;
148
+
149
+ if ( place . geometry && place . geometry . location ) {
150
+ germanAddress . geoLocation . latitude = place . geometry . location . lat ( ) ;
151
+ germanAddress . geoLocation . longitude = place . geometry . location . lng ( ) ;
152
+ }
153
+
154
+ place . address_components . forEach ( value => {
155
+ if ( value . types . indexOf ( 'street_number' ) > - 1 ) {
156
+ germanAddress . streetNumber = value . short_name ;
157
+ }
158
+ if ( value . types . indexOf ( 'route' ) > - 1 ) {
159
+ germanAddress . streetName = value . long_name ;
160
+ }
161
+ if ( value . types . indexOf ( 'postal_code' ) > - 1 ) {
162
+ germanAddress . postalCode = Number ( value . short_name ) ;
163
+ }
164
+ if ( value . types . indexOf ( 'sublocality' ) > - 1 ) {
165
+ germanAddress . sublocality = value . long_name ;
166
+ }
167
+ if ( value . types . indexOf ( 'locality' ) > - 1 ) {
168
+ germanAddress . locality . long = value . long_name ;
169
+ germanAddress . locality . short = value . short_name ;
170
+ }
171
+ if ( value . types . indexOf ( 'administrative_area_level_1' ) > - 1 ) {
172
+ germanAddress . state . long = value . long_name ;
173
+ germanAddress . state . short = value . short_name ;
174
+ }
175
+ if ( value . types . indexOf ( 'country' ) > - 1 ) {
176
+ germanAddress . country . long = value . long_name ;
177
+ germanAddress . country . short = value . short_name ;
178
+ }
179
+ if ( value . types . indexOf ( 'administrative_area_level_3' ) > - 1 ) {
180
+ germanAddress . locality . short = value . short_name ;
181
+ }
196
182
} ) ;
197
- } )
198
- . catch ( ( err ) => console . log ( err ) ) ;
183
+
184
+ this . onGermanAddressMapped . emit ( germanAddress ) ;
185
+
186
+ this . value = place . formatted_address ;
187
+ this . address = place . formatted_address ;
188
+ this . onAutocompleteSelected . emit ( place ) ;
189
+ this . onLocationSelected . emit (
190
+ {
191
+ latitude : place . geometry . location . lat ( ) ,
192
+ longitude : place . geometry . location . lng ( )
193
+ } ) ;
194
+ } ) ;
195
+ } ) ;
199
196
}
200
197
201
198
registerOnChange ( fn : any ) : void {
@@ -215,4 +212,40 @@ export class MatGoogleMapsAutocompleteDirective implements OnInit, ControlValueA
215
212
}
216
213
}
217
214
215
+ loadMap ( ) : void {
216
+ this . loaderService . loadScript ( `https://maps.googleapis.com/maps/api/js?key=${ this . apiKey } &libraries=places` )
217
+ . then ( ( ) => {
218
+ this . initMap ( ) ;
219
+ } )
220
+ . catch ( error => console . error ( 'Google Maps loading failed: ' , error ) ) ;
221
+ }
222
+
223
+ initMap ( ) {
224
+ if ( isPlatformBrowser ( this . platformId ) ) {
225
+
226
+ console . log ( "on after view init --> " , this . elemRef . nativeElement )
227
+
228
+ this . autocomplete = new google . maps . places . Autocomplete (
229
+ this . elemRef . nativeElement
230
+ ) ;
231
+
232
+ const options : AutocompleteOptions = {
233
+ // types: ['address'],
234
+ // componentRestrictions: {country: this.country},
235
+ placeIdOnly : this . placeIdOnly ,
236
+ strictBounds : this . strictBounds ,
237
+ // types: this.types,
238
+ type : this . type
239
+ } ;
240
+
241
+ // tslint:disable-next-line:no-unused-expression
242
+ this . country ? options . componentRestrictions = { country : this . country } : null ;
243
+ // tslint:disable-next-line:no-unused-expression
244
+ this . country ? options . types = this . types : null ;
245
+
246
+ this . autoCompleteOptions = Object . assign ( this . autoCompleteOptions , options ) ;
247
+ this . initGoogleMapsAutocomplete ( ) ;
248
+ }
249
+ }
250
+
218
251
}
0 commit comments