1
+ angular . module ( "umbraco" )
2
+ . controller ( "Bergmania.OpenStreetMap.Controller" , [ "$scope" , "$element" , function ( $scope , $element ) {
3
+ const vm = this ;
4
+
5
+ vm . currentMarker = null ;
6
+
7
+ function onInit ( ) {
8
+
9
+ const initValue = $scope . model . value || $scope . model . config . defaultPosition || { marker : { latitude : 54.975556 , longitude : - 1.621667 } , "boundingBox" :{ "southWestCorner" :{ "latitude" :54.970495269313204 , "longitude" :- 1.6278648376464846 } , "northEastCorner" :{ "latitude" :54.97911600936982 , "longitude" :- 1.609625816345215 } } , zoom : 16 } ;
10
+ const tileLayer = $scope . model . config . tileLayer || 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png' ;
11
+ const tileLayerOptions = { attribution :$scope . model . config . tileLayerAttribution } ;
12
+
13
+ vm . map = L . map ( $element . find ( "[data-openstreetmap]" ) [ 0 ] )
14
+ . fitBounds ( L . latLngBounds ( L . latLng ( initValue . boundingBox . southWestCorner . latitude , initValue . boundingBox . southWestCorner . longitude ) ,
15
+ L . latLng ( initValue . boundingBox . northEastCorner . latitude , initValue . boundingBox . northEastCorner . longitude ) ) ) ;
16
+
17
+ L . tileLayer ( tileLayer , tileLayerOptions ) . addTo ( vm . map ) ;
18
+
19
+ vm . map . on ( 'click' , onMapClick ) ;
20
+ vm . map . on ( 'moveend' , updateModel ) ;
21
+ vm . map . on ( 'zoomend' , updateModel ) ;
22
+ vm . map . on ( 'contextmenu' , clearMarker ) ;
23
+
24
+ if ( initValue . marker ) {
25
+ vm . currentMarker = L . marker ( L . latLng ( initValue . marker . latitude , initValue . marker . longitude ) , { draggable :true , } ) . addTo ( vm . map ) ;
26
+ }
27
+ }
28
+ function clearMarker ( ) {
29
+ if ( vm . currentMarker ) {
30
+ vm . currentMarker . remove ( vm . map ) ;
31
+ vm . currentMarker = null ;
32
+ }
33
+
34
+ updateModel ( ) ;
35
+ }
36
+
37
+ function onMapClick ( e ) {
38
+ clearMarker ( ) ;
39
+
40
+ vm . map . setView ( e . latlng ) ;
41
+ vm . currentMarker = L . marker ( e . latlng , { draggable :true , } ) . addTo ( vm . map ) ;
42
+
43
+ updateModel ( e ) ;
44
+ }
45
+
46
+ function updateModel ( ) {
47
+
48
+ $scope . model . value = { } ;
49
+
50
+ $scope . model . value . zoom = vm . map . getZoom ( ) ;
51
+
52
+ if ( ! $scope . model . value . boundingBox ) {
53
+ $scope . model . value . boundingBox = { } ;
54
+ }
55
+ if ( ! $scope . model . value . boundingBox . southWestCorner ) {
56
+ $scope . model . value . boundingBox . southWestCorner = { } ;
57
+ }
58
+ if ( ! $scope . model . value . boundingBox . northEastCorner ) {
59
+ $scope . model . value . boundingBox . northEastCorner = { } ;
60
+ }
61
+
62
+ const northEastCorner = vm . map . getBounds ( ) . getNorthEast ( ) ;
63
+ const southWestCorner = vm . map . getBounds ( ) . getSouthWest ( ) ;
64
+
65
+ $scope . model . value . boundingBox . northEastCorner . latitude = northEastCorner . lat ;
66
+ $scope . model . value . boundingBox . northEastCorner . longitude = northEastCorner . lng ;
67
+ $scope . model . value . boundingBox . southWestCorner . latitude = southWestCorner . lat ;
68
+ $scope . model . value . boundingBox . southWestCorner . longitude = southWestCorner . lng ;
69
+
70
+ if ( vm . currentMarker ) {
71
+ const marker = vm . currentMarker . getLatLng ( ) ;
72
+
73
+ if ( ! $scope . model . value . marker ) {
74
+ $scope . model . value . marker = { } ;
75
+ }
76
+ $scope . model . value . marker . latitude = marker . lat ;
77
+ $scope . model . value . marker . longitude = marker . lng ;
78
+
79
+ } else {
80
+ $scope . model . value . marker = null ;
81
+ }
82
+
83
+
84
+ }
85
+ onInit ( ) ;
86
+ } ] ) ;
0 commit comments