allerta-vvf/frontend/src/app/_components/map-picker/map-picker.component.ts

125 lines
3.5 KiB
TypeScript
Raw Normal View History

2022-01-07 18:37:00 +01:00
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
2022-01-07 16:41:09 +01:00
import { ToastrService } from 'ngx-toastr';
2022-03-01 00:07:51 +01:00
import { TranslateService } from '@ngx-translate/core';
2022-01-07 16:41:09 +01:00
import { ApiClientService } from 'src/app/_services/api-client.service';
2024-01-02 00:38:30 +01:00
import { LatLng, latLng, tileLayer, Marker, Map, Icon } from 'leaflet';
2022-01-07 16:41:09 +01:00
import "leaflet.locatecontrol";
@Component({
selector: 'map-picker',
templateUrl: './map-picker.component.html',
styleUrls: ['./map-picker.component.scss']
})
export class MapPickerComponent implements OnInit {
2022-02-24 23:41:46 +01:00
lat = 45.88283872530;
lng = 10.18226623535;
@Input() selectLat = "";
@Input() selectLng = "";
2022-01-07 18:37:00 +01:00
2023-12-31 15:30:39 +01:00
@Output() markerSet = new EventEmitter<any>();
2022-01-07 18:37:00 +01:00
2022-01-07 16:41:09 +01:00
options = {
layers: [
tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { maxZoom: 19, attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors' })
2022-01-07 16:41:09 +01:00
],
zoom: 10,
2022-01-07 18:37:00 +01:00
center: latLng(this.lat, this.lng)
2022-01-07 16:41:09 +01:00
};
isMarkerSet = false;
marker: Marker;
map: Map;
lc: any;
placeName = "";
isPlaceSearchResultsOpen = false;
placeSearchResults: any[] = [];
2022-03-01 00:07:51 +01:00
constructor(private toastr: ToastrService, private api: ApiClientService, private translate: TranslateService) {
2022-01-07 16:41:09 +01:00
this.marker = (window as any).L.marker(latLng(0,0));
this.map = undefined as unknown as Map;
}
2022-02-24 23:41:46 +01:00
ngOnInit() {
if(this.selectLat !== "" && this.selectLng !== "") {
console.log(this.selectLat, this.selectLng);
this.setMarker(latLng(parseFloat(this.selectLat), parseFloat(this.selectLng)));
}
}
2022-01-07 16:41:09 +01:00
2022-01-07 18:37:00 +01:00
setMarker(latLng: LatLng) {
2023-12-31 15:30:39 +01:00
this.markerSet.emit({
2022-01-07 18:37:00 +01:00
lat: latLng.lat,
lng: latLng.lng
});
2022-01-07 16:41:09 +01:00
const iconRetinaUrl = "./assets/icons/marker-icon-2x.png";
const iconUrl = "./assets/icons/marker-icon.png";
const shadowUrl = "./assets/icons/marker-shadow.png";
2024-01-02 00:38:30 +01:00
const iconDefault = new Icon({
2022-01-07 16:41:09 +01:00
iconRetinaUrl,
iconUrl,
shadowUrl,
iconSize: [25, 41],
iconAnchor: [12, 41],
popupAnchor: [1, -34],
tooltipAnchor: [16, -28],
shadowSize: [41, 41]
});
this.marker.remove();
2022-01-07 18:37:00 +01:00
this.marker = (window as any).L.marker(latLng, { icon: iconDefault });
2022-01-07 16:41:09 +01:00
this.isMarkerSet = true;
}
mapReady(map: any) {
this.map = map;
const this_class = this;
(window as any).L.Control.CustomLocate = (window as any).L.Control.Locate.extend({
_drawMarker: function () {
this_class.setMarker(this._event.latlng);
},
_onDrag: function () {},
_onZoom: function () {},
_onZoomEnd: function () {}
});
this.lc = new (window as any).L.Control.CustomLocate({
cacheLocation: false, // disabled for privacy reasons
initialZoomLevel: 16
}).addTo(map);
}
mapClick(e: any) {
console.log(e);
this.setMarker(e.latlng);
}
searchPlace() {
if(this.placeName.length < 3) {
2024-01-08 23:19:37 +01:00
this.translate.get('validation.place_min_length').subscribe((res: string) => {
2022-03-01 00:07:51 +01:00
this.toastr.error(res);
});
2022-01-07 16:41:09 +01:00
return;
}
2022-01-08 00:20:23 +01:00
this.api.get("places/search", {
2022-01-07 16:41:09 +01:00
q: this.placeName
}).then((places) => {
this.isPlaceSearchResultsOpen = true;
this.placeSearchResults = places;
}).catch((err) => {
console.error(err);
2022-03-01 00:07:51 +01:00
this.translate.get('map_picker.loading_error').subscribe((res: string) => {
this.toastr.error(res);
});
2022-01-07 16:41:09 +01:00
});
}
selectPlace(place: any) {
console.log(place);
let latlng = latLng(place.lat, place.lon);
this.setMarker(latlng);
this.map.setView(latlng, 16);
}
}