diff --git a/frontend/app/shared/components/geolocation-editor.component.ts b/frontend/app/shared/components/geolocation-editor.component.ts index 2ce7e0ca1..3763f4391 100644 --- a/frontend/app/shared/components/geolocation-editor.component.ts +++ b/frontend/app/shared/components/geolocation-editor.component.ts @@ -33,6 +33,8 @@ interface Snapshot { isMapHidden?: boolean; } +type UpdateOptions = { reset?: boolean; pan?: true; fire?: boolean }; + @Component({ selector: 'sqx-geolocation-editor', styleUrls: ['./geolocation-editor.component.scss'], @@ -101,7 +103,7 @@ export class GeolocationEditorComponent extends StatefulControlComponent any = isDisabled ? - x => x.enable() : - x => x.disable(); + x => x.disable() : + x => x.enable(); if (this.map) { update(this.map.zoomControl); @@ -159,7 +161,7 @@ export class GeolocationEditorComponent extends StatefulControlComponent { + + this.resourceLoader.loadScript('https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.3/leaflet.js') + .then(() => { this.map = L.map(this.editor.nativeElement).fitWorld(); L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', @@ -187,17 +191,32 @@ export class GeolocationEditorComponent extends StatefulControlComponentOpenStreetMap contributors' }).addTo(this.map); - this.map.on('click', - (event: any) => { - if (!this.marker && !this.snapshot.isDisabled) { - const latlng = event.latlng.wrap(); - - this.updateValue(latlng.lat, latlng.lng); - this.updateMarker(false, true); - } + this.resourceLoader.loadScript('https://cdnjs.cloudflare.com/ajax/libs/perliedman-leaflet-control-geocoder/1.9.0/Control.Geocoder.min.js') + .then(() => { + L.Control.geocoder({ + defaultMarkGeocode: false + }) + .on('markgeocode', (event: any) => { + const center = event.geocode.center; + + if (!this.snapshot.isDisabled) { + this.updateValue(center.lat, center.lng); + this.updateMarker({ reset: true, fire: true }); + } + }) + .addTo(this.map); }); - this.updateMarker(true, false); + this.map.on('click', (event: any) => { + if (!this.snapshot.isDisabled) { + const latlng = event.latlng.wrap(); + + this.updateValue(latlng.lat, latlng.lng); + this.updateMarker({ fire: true }); + } + }); + + this.updateMarker({ reset: true }); if (this.snapshot.isDisabled) { this.map.zoomControl.disable(); @@ -210,25 +229,28 @@ export class GeolocationEditorComponent extends StatefulControlComponent { + this.resourceLoader.loadScript(`https://maps.googleapis.com/maps/api/js?key=${apiKey}&libraries=places`) + .then(() => { this.map = new google.maps.Map(this.editor.nativeElement, { zoom: 1, + fullscreenControl: false, mapTypeControl: false, + mapTypeControlOptions: {}, streetViewControl: false, center: { lat: 0, lng: 0 } }); const searchBox = new google.maps.places.SearchBox(this.searchBoxInput.nativeElement); - this.map.addListener('click', - (event: any) => { - if (!this.snapshot.isDisabled) { - this.updateValue(event.latLng.lat(), event.latLng.lng()); - this.updateMarker(false, true); - } - }); + this.map.addListener('click', (event: any) => { + if (!this.snapshot.isDisabled) { + const latlng = event.latLng; + + this.updateValue(latlng.lat(), latlng.lng()); + this.updateMarker({ fire: true }); + } + }); this.map.addListener('bounds_changed', () => { searchBox.setBounds(this.map.getBounds()); @@ -249,12 +271,12 @@ export class GeolocationEditorComponent extends StatefulControlComponent { - const latlng = event.latlng.wrap(); + this.marker.on('dragend', (event: any) => { + const latlng = event.target.getLatLng().wrap(); this.updateValue(latlng.lat, latlng.lng); - }); - - this.marker.on('dragend', () => { - this.updateMarker(false, true); + this.updateMarker({ fire: true }); }); if (this.snapshot.isDisabled) { @@ -314,9 +334,9 @@ export class GeolocationEditorComponent extends StatefulControlComponent { - if (!this.snapshot.isDisabled) { - this.updateValue(event.latLng.lat(), event.LatLng.lng()); - } - }); this.marker.addListener('dragend', (event: any) => { if (!this.snapshot.isDisabled) { - this.updateValue(event.latLng.lat(), event.LatLng.lng()); - this.updateMarker(false, true); + const latlng = event.latLng; + + this.updateValue(latlng.lat(), latlng.lng()); + this.updateMarker({ fire: true }); } }); } - const latLng = { lat: this.value.latitude, lng: this.value.longitude }; + const latLng = new google.maps.LatLng(this.value.latitude, this.value.longitude); - if (zoom) { + if (opts && opts.reset) { + this.map.setZoom(12); + this.map.setCenter(latLng); + } else if (opts && opts.pan) { this.map.setCenter(latLng); - } else { - this.map.panTo(latLng); } this.marker.setPosition(latLng); - this.map.setZoom(12); } else { if (this.marker) { this.marker.setMap(null); this.marker = null; } - - this.map.setCenter({ lat: 0, lng: 0 }); } } } \ No newline at end of file