-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMapPicker.php
122 lines (116 loc) · 3.81 KB
/
MapPicker.php
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
namespace proximitymad\yii2mapspickerwidget;
use yii\base\Widget;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\web\View;
/**
* Class MapPicker Widget
*/
class MapPicker extends Widget
{
/**
* @var bool|string Unique id for the map, optional
*/
public $mapId = false;
/**
* @var bool|int Width for the map widget, optional
*/
public $width = "200px";
/**
* @var int Height for the map widget, optional
*/
public $height = "100px";
/**
* @var string Input field where to update matching latitude
*/
public $latFieldClass = 'field-lat';
/**
* @var string Input field where to update matching longitude
*/
public $lngFieldClass = 'field-lng';
/**
* @var string Selector for error message
*/
public $errorClass = 'search-error';
/**
* @var string
*/
public $errorMsg = 'No results founds';
/**
* @var bool
*/
public $search = false;
/**
* @var array
*/
public $searchField = [
'inputClass' => 'search-field',
'buttonClass' => 'btn-search'
];
public $mapOptions = [
'zoom' => 16,
'streetViewControl' => false
];
public $options = [];
/**
* @var bool Wether to load the Google Maps API file or not in case it is already included
*/
public $loadMapApi = true;
/**
* @var bool|string Google Map API key, required if there is not an Google Maps API already loaded
*/
public $apiKey = false;
/**
* @var bool Defines if the widget must load automatically
*/
public $show = true;
/**
* @var null Defines where to load the JS @see yii\web\View
*/
public $scriptsPosition = null;
public function init()
{
$this->scriptsPosition = $this->scriptsPosition?:View::POS_READY;
if ($this->loadMapApi === true && ($this->apiKey === false || $this->apiKey === '')) {
throw new \Exception("Api key missing");
}
$asset = MapPickerAsset::register($this->view);
if ($this->loadMapApi) {
$asset->loadMapsLib($this->apiKey);
}
$this->mapId = $this->mapId ?: 'map_' . substr(md5(time() . rand(0, 999)), 0, 8);
$styles = [];
$width = strpos($this->width, "px")||strpos($this->width, "%")?$this->width:$this->width."px";
$height = strpos($this->height, "px")||strpos($this->height, "%")?$this->height:$this->height."px";
$styles[] = $this->width ? "width: {$width};" : '';
$styles[] = $this->height ? "height: {$height};" : '';
$style = implode(" ", $styles);
$this->options = ArrayHelper::merge(['style' => $style], $this->options);
}
public function run()
{
$options = [
'mapId' => $this->mapId,
'latField' => $this->latFieldClass,
'lngField' => $this->lngFieldClass,
'error' => $this->errorClass,
'mapOptions' => $this->mapOptions,
'searchField' => $this->searchField,
'errorMsg' => $this->errorMsg
];
$js = "var mapPicker_{$this->mapId} = new MapPicker(" . json_encode($options) . ")\n";
$js .= "var mapInstances = []; mapInstances.push(mapPicker_{$this->mapId})\n";
$js .= "mapPicker_{$this->mapId}.init()\n";
if($this->show){
$js .= "mapPicker_{$this->mapId}.init()\n";
if ($this->search) {
$js .= "mapPicker_{$this->mapId}.search('{$this->search}')\n";
} else {
throw new \Exception("MapPicker: 'search' parameter is required");
}
}
$this->view->registerJs($js, $this->scriptsPosition);
return Html::tag('div', 'Map', ArrayHelper::merge(['id' => $this->mapId], $this->options));
}
}