This repository was archived by the owner on Nov 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect-location.component.ts
More file actions
88 lines (68 loc) · 2.33 KB
/
Copy pathselect-location.component.ts
File metadata and controls
88 lines (68 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { Component, OnInit, Input, Output, ViewChild, ElementRef, EventEmitter } from '@angular/core';
import { inRange } from 'lodash';
import { LatLng, Map, TileLayer } from 'leaflet';
import 'leaflet';
import { NotificationsService } from 'app/notifications/notifications.service';
@Component({
selector: 'app-select-location',
templateUrl: './select-location.component.html',
styleUrls: ['./select-location.component.scss']
})
export class SelectLocationComponent implements OnInit {
@ViewChild('locationContainer')
locationContainer: ElementRef;
private map: Map;
@Input()
public location: [number, number];
@Input() disabled = false;
// tslint:disable-next-line:no-output-on-prefix
@Output() public submit = new EventEmitter<[number, number]>();
constructor(private notify: NotificationsService) { }
ngOnInit() {
const location = (this.location) ? this.location : [0, 0];
const zoom = (this.location) ? 8 : 1;
this.map = new Map(this.locationContainer.nativeElement, {
center: new LatLng(location[0], location[1]),
zoom,
scrollWheelZoom: 'center', // zoom to the center point
layers: [
new TileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 18,
attribution: 'Open Street Map',
noWrap: true
})
],
attributionControl: false
});
// if the center of the map gets outside of the world, move it back
this.map.on('moveend', () => {
let center = this.map.getCenter();
while (!inRange(center.lng, -180, 180)) {
let moveBy;
if (center.lng > 180) {
moveBy = -360;
} else {
moveBy = 360;
}
this.map.setView([center.lat, center.lng + moveBy],
this.map.getZoom(),
{ animate: false });
center = this.map.getCenter();
}
});
}
public updateLocation() {
// disable the button while the location is updated
const { lat, lng } = this.map.getCenter();
const location: [number, number] = [lat, lng];
// prevent saving the null island
if (location[0] === 0 && location[1] === 0) {
this.notify.error('Please choose a non-default location.');
return;
}
this.submit.emit(location);
}
public removeLocation() {
this.submit.emit(null);
}
}