|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bavix\Leaflet; |
|
4
|
|
|
|
|
5
|
|
|
use Bavix\Leaflet\Tiles\Sputnik; |
|
6
|
|
|
use Encore\Admin\Form\Field; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
|
|
9
|
|
|
class LeafletMap extends Field |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var array |
|
14
|
|
|
*/ |
|
15
|
|
|
protected static $css = [ |
|
16
|
|
|
'https://unpkg.com/[email protected]/dist/leaflet.css', |
|
17
|
|
|
'https://unpkg.com/[email protected]/assets/css/leaflet.css', |
|
18
|
|
|
]; |
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected static $js = [ |
|
23
|
|
|
'https://unpkg.com/[email protected]/dist/leaflet.js', |
|
24
|
|
|
'https://unpkg.com/[email protected]/dist/bundle.min.js', |
|
25
|
|
|
]; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var Tile |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $tile; |
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $view = 'laravel-admin-leaflet::leaflet'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* LeafletMap constructor. |
|
37
|
|
|
* @param $column |
|
38
|
|
|
* @param array $arguments |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct($column, array $arguments) |
|
41
|
|
|
{ |
|
42
|
|
|
$column = [ |
|
43
|
|
|
'lat' => $column, |
|
44
|
|
|
'lng' => \array_shift($arguments), |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
|
|
if (empty($arguments)) { |
|
48
|
|
|
$arguments = ['Leaflet']; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$this->options((array)\config('admin.extensions.leaflet.config')); |
|
52
|
|
|
parent::__construct($column, $arguments); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string |
|
57
|
|
|
*/ |
|
58
|
|
|
public function render() |
|
59
|
|
|
{ |
|
60
|
|
|
$uuid = Str::uuid(); |
|
61
|
|
|
$this->variables['uuid'] = $uuid; |
|
62
|
|
|
|
|
63
|
|
|
$this->script = <<<script |
|
64
|
|
|
var latitude = document.getElementById('{$uuid}{$this->id['lat']}'); |
|
65
|
|
|
var longitude = document.getElementById('{$uuid}{$this->id['lng']}'); |
|
66
|
|
|
var map = L.map('$uuid').setView([latitude.value, longitude.value], {$this->zoom()}); |
|
67
|
|
|
var marker = L.marker([latitude.value, longitude.value]).addTo(map); |
|
68
|
|
|
|
|
69
|
|
|
L.tileLayer('{$this->getTile()->layer()}', {$this->tileOptions()}).addTo(map); |
|
70
|
|
|
|
|
71
|
|
|
var searchControl = new GeoSearch.GeoSearchControl({ |
|
72
|
|
|
provider: new GeoSearch.{$this->provider()}({$this->providerParams()}), |
|
73
|
|
|
style: '{$this->style()}', |
|
74
|
|
|
}); |
|
75
|
|
|
|
|
76
|
|
|
map.addControl(searchControl); |
|
77
|
|
|
map.on('geosearch/showlocation', function (e) { |
|
78
|
|
|
latitude.value = e.location.x; |
|
79
|
|
|
longitude.value = e.location.y; |
|
80
|
|
|
}); |
|
81
|
|
|
script; |
|
82
|
|
|
|
|
83
|
|
|
return parent::render(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return int |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function zoom(): int |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->options['zoom'] ?? ($this->getTile()->maxZoom() - 1); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return Tile |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function getTile(): Tile |
|
98
|
|
|
{ |
|
99
|
|
|
if (!$this->tile) { |
|
100
|
|
|
$class = $this->options['tile'] ?? Sputnik::class; |
|
101
|
|
|
$this->tile = new $class(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $this->tile; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function tileOptions(): string |
|
111
|
|
|
{ |
|
112
|
|
|
return \json_encode([ |
|
113
|
|
|
'attribution' => $this->getTile()->attribution(), |
|
114
|
|
|
'maxZoom' => $this->getTile()->maxZoom(), |
|
115
|
|
|
]); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return string |
|
120
|
|
|
*/ |
|
121
|
|
|
protected function provider(): string |
|
122
|
|
|
{ |
|
123
|
|
|
switch ($this->options['geoProvider'] ?? '') { |
|
124
|
|
|
case 'bing': |
|
125
|
|
|
return 'BingProvider'; |
|
126
|
|
|
case 'esri': |
|
127
|
|
|
return 'EsriProvider'; |
|
128
|
|
|
case 'google': |
|
129
|
|
|
return 'GoogleProvider'; |
|
130
|
|
|
case 'locationIQ': |
|
131
|
|
|
return 'LocationIQProvider'; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return 'OpenStreetMapProvider'; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return string |
|
139
|
|
|
*/ |
|
140
|
|
|
protected function providerParams(): string |
|
141
|
|
|
{ |
|
142
|
|
|
switch ($this->options['geoProvider'] ?? '') { |
|
143
|
|
|
case 'bing': |
|
144
|
|
|
case 'google': |
|
145
|
|
|
case 'locationIQ': |
|
146
|
|
|
return json_encode([ |
|
147
|
|
|
'params' => [ |
|
148
|
|
|
'key' => config('admin.extensions.leaflet.keys.' . $this->options['geoProvider']) |
|
149
|
|
|
] |
|
150
|
|
|
]); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return ''; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @return string |
|
158
|
|
|
*/ |
|
159
|
|
|
protected function style(): string |
|
160
|
|
|
{ |
|
161
|
|
|
return $this->options['style'] ?? 'bar'; |
|
|
|
|
|
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
|