@@ -11,6 +11,7 @@ export default function filamentGoogleMapsField(
1111 draggable,
1212 clickable,
1313 defaultLocation,
14+ statePath,
1415 controls,
1516 layers,
1617 reverseGeocodeFields,
@@ -36,6 +37,10 @@ export default function filamentGoogleMapsField(
3637 geoJsonField,
3738 geoJsonProperty,
3839 geoJsonVisible,
40+ reverseGeocodeUsing,
41+ placeUpdatedUsing,
42+ hasReverseGeocodeUsing = false ,
43+ hasPlaceUpdatedUsing = false ,
3944 }
4045) {
4146 return {
@@ -82,6 +87,8 @@ export default function filamentGoogleMapsField(
8287 // zIndex: 1,
8388 } ,
8489 selectedShape : null ,
90+ placesService : null ,
91+ placeFields : [ ] ,
8592
8693 loadGMaps : function ( ) {
8794 if ( ! document . getElementById ( 'filament-google-maps-google-maps-js' ) ) {
@@ -154,6 +161,20 @@ export default function filamentGoogleMapsField(
154161 } )
155162 }
156163
164+ if ( hasPlaceUpdatedUsing ) {
165+ this . placesService = new google . maps . places . PlacesService ( this . map ) ;
166+ }
167+
168+ this . placeFields = [ "address_components" , "formatted_address" , "geometry" , "name" ] ;
169+
170+ if ( ! this . placeFields . includes ( placeField ) ) {
171+ this . placeFields . push ( placeField ) ;
172+ }
173+
174+ if ( hasPlaceUpdatedUsing ) {
175+ this . placeFields . push ( "photos" ) ;
176+ }
177+
157178 if ( autocomplete ) {
158179 const geoComplete = document . getElementById ( autocomplete ) ;
159180
@@ -167,14 +188,8 @@ export default function filamentGoogleMapsField(
167188 }
168189 } , true ) ;
169190
170- let fields = [ "address_components" , "formatted_address" , "geometry" , "name" ] ;
171-
172- if ( ! fields . includes ( placeField ) ) {
173- fields . push ( placeField ) ;
174- }
175-
176191 const geocompleteOptions = {
177- fields : fields ,
192+ fields : this . placeFields ,
178193 strictBounds : false ,
179194 types : types ,
180195 } ;
@@ -203,7 +218,10 @@ export default function filamentGoogleMapsField(
203218 this . marker . setPosition ( place . geometry . location ) ;
204219 this . markerLocation = place . geometry . location ;
205220 this . setCoordinates ( place . geometry . location ) ;
206- this . updateGeocode ( place . address_components ) ;
221+ this . updateGeocodeFromAddressComponents ( place . address_components ) ;
222+ if ( hasPlaceUpdatedUsing ) {
223+ placeUpdatedUsing ( place ) ;
224+ }
207225 } ) ;
208226 }
209227 }
@@ -329,7 +347,7 @@ export default function filamentGoogleMapsField(
329347 const markerLocation = this . marker . getPosition ( ) ;
330348
331349 if ( ! ( location . lat === markerLocation . lat ( ) && location . lng === markerLocation . lng ( ) ) ) {
332- this . updateAutocomplete ( location )
350+ this . updateAutocompleteFromLocation ( location )
333351 this . updateMap ( location ) ;
334352 }
335353 } )
@@ -338,47 +356,80 @@ export default function filamentGoogleMapsField(
338356 this . geoJsonContains ( event . latLng ) ;
339357 this . markerLocation = event . latLng . toJSON ( ) ;
340358 this . setCoordinates ( this . markerLocation ) ;
341- this . updateAutocomplete ( this . markerLocation ) ;
342- this . updateGeocodeFromLocation ( this . markerLocation ) ;
343- // this.updateMap(this.markerLocation);
359+ this . updateFromLocation ( this . markerLocation ) ;
344360 this . map . panTo ( this . markerLocation ) ;
361+
362+ if ( hasPlaceUpdatedUsing && event . placeId ) {
363+ this . placesService . getDetails (
364+ {
365+ placeId : event . placeId ,
366+ fields : this . placeFields
367+ } ,
368+ ( results , status ) => {
369+ status === 'OK' && placeUpdatedUsing ( results ) ;
370+ }
371+ ) ;
372+ }
345373 } ,
346374 updateMap : function ( position ) {
347375 this . marker . setPosition ( position ) ;
348376 this . map . panTo ( position ) ;
349377 } ,
350- updateGeocode : function ( address_components ) {
351- const replacements = this . getReplacements ( address_components ) ;
378+ updateFromLocation : function ( location ) {
379+ if ( this . hasReverseGeocode ( ) || this . hasReverseAutocomplete ( ) ) {
380+ this . geocoder
381+ . geocode ( { location} )
382+ . then ( ( response ) => {
383+ this . updateGeocodeFromAddressComponents ( response . results [ 0 ] . address_components )
384+ this . updateAutocompleteFromFormattedAddress ( response . results [ 0 ] . formatted_address )
385+ if ( hasReverseGeocodeUsing ) {
386+ reverseGeocodeUsing ( response ) ;
387+ }
388+ } )
389+ . catch ( ( error ) => {
390+ console . log ( error . message ) ;
391+ } )
392+ }
393+ } ,
394+ updateGeocodeFromAddressComponents : function ( address_components ) {
395+ if ( this . hasReverseGeocode ( ) ) {
396+ const replacements = this . getReplacements ( address_components ) ;
397+
398+ for ( const field in reverseGeocodeFields ) {
399+ let replaced = reverseGeocodeFields [ field ] ;
400+ for ( const replacement in replacements ) {
401+ replaced = replaced . split ( replacement ) . join ( replacements [ replacement ] ) ;
402+ }
352403
353- for ( const field in reverseGeocodeFields ) {
354- let replaced = reverseGeocodeFields [ field ] ;
355- for ( const replacement in replacements ) {
356- replaced = replaced . split ( replacement ) . join ( replacements [ replacement ] ) ;
357- }
404+ for ( const symbol in this . symbols ) {
405+ replaced = replaced . split ( symbol ) . join ( '' ) ;
406+ }
358407
359- for ( const symbol in this . symbols ) {
360- replaced = replaced . split ( symbol ) . join ( '' ) ;
408+ replaced = replaced . trim ( ) ;
409+ setStateUsing ( field , replaced )
361410 }
362-
363- replaced = replaced . trim ( ) ;
364- setStateUsing ( field , replaced )
365411 }
366412 } ,
367413 updateGeocodeFromLocation : function ( location ) {
368- if ( Object . keys ( reverseGeocodeFields ) . length > 0 ) {
414+ if ( this . hasReverseGeocode ( ) ) {
369415 this . geocoder
370- . geocode ( { location } )
416+ . geocode ( { location} )
371417 . then ( ( response ) => response . results [ 0 ] . address_components )
372- . then ( ( address_components ) => this . updateGeocode ( address_components ) )
418+ . then ( ( address_components ) => this . updateGeocodeFromAddressComponents ( address_components ) )
373419 . catch ( ( error ) => {
374420 console . log ( error . message ) ;
375421 } )
376422 }
377423 } ,
378- updateAutocomplete : function ( position ) {
379- if ( autocomplete && autocompleteReverse ) {
424+ updateAutocompleteFromFormattedAddress : function ( address ) {
425+ if ( this . hasReverseAutocomplete ( ) ) {
426+ setStateUsing ( autocomplete , address ) ;
427+ }
428+ } ,
429+ updateAutocompleteFromLocation : function ( location ) {
430+ if ( this . hasReverseAutocomplete ( ) ) {
380431 this . geocoder
381- . geocode ( { location : position } )
432+ . geocode ( { location : location } )
382433 . then ( ( response ) => {
383434 if ( response . results [ 0 ] ) {
384435 setStateUsing ( autocomplete , response . results [ 0 ] . formatted_address ) ;
@@ -389,6 +440,12 @@ export default function filamentGoogleMapsField(
389440 } )
390441 }
391442 } ,
443+ hasReverseAutocomplete : function ( ) {
444+ return autocomplete && autocompleteReverse
445+ } ,
446+ hasReverseGeocode : function ( ) {
447+ return Object . keys ( reverseGeocodeFields ) . length > 0 || reverseGeocodeUsing
448+ } ,
392449 setCoordinates : function ( position ) {
393450 this . state = position ;
394451 } ,
@@ -405,8 +462,7 @@ export default function filamentGoogleMapsField(
405462 lng : position . coords . longitude
406463 } ;
407464 this . setCoordinates ( this . markerLocation ) ;
408- this . updateAutocomplete ( this . markerLocation ) ;
409- this . updateGeocodeFromLocation ( this . markerLocation ) ;
465+ this . updateFromLocation ( this . markerLocation ) ;
410466 this . map . panTo ( this . markerLocation ) ;
411467 } ) ;
412468 } ,
0 commit comments