Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.76 KB | None | 0 0
  1. <?php
  2.  
  3. namespace ibrarturilatlngfinder;
  4.  
  5. use yiiwebView;
  6.  
  7. /**
  8. * A widget to find Latitude and Longitude using Google Maps for Yii Framework 2
  9. * by Ibrar Turi
  10. *
  11. * @see https://github.com/ibrarturi/yii2-latlon-finder
  12. * @author Ibrar Turi <ibrarturi@gmail.com>
  13. * @since 1.0
  14. */
  15. class LatLngFinder extends yiibaseWidget
  16. {
  17. /**
  18. * @var string $latAttribute Latitude attribute id
  19. */
  20. public $latAttribute = null;
  21.  
  22. /**
  23. * @var string $lngAttribute Longitude attribute id
  24. */
  25. public $lngAttribute = null;
  26.  
  27. /**
  28. * @var string $zoomAttribute Zomm attribute id
  29. */
  30. public $zoomAttribute = null;
  31.  
  32. /**
  33. * @var string $mapCanvasId Map canvas id
  34. */
  35. public $mapCanvasId = null;
  36.  
  37. /**
  38. * @var integer $mapWidth Width of the map canvas
  39. */
  40. public $mapWidth = null;
  41.  
  42. /**
  43. * @var integer $mapHeight Height of the map canvas
  44. */
  45. public $mapHeight = null;
  46.  
  47. /**
  48. * @var float $defaultLat Default Latitude for the map
  49. */
  50. public $defaultLat = null;
  51.  
  52. /**
  53. * @var float $defaultLng Default Longitude for the map
  54. */
  55. public $defaultLng = null;
  56.  
  57. /**
  58. * @var integer $defaultZoom Default initial Zoom for the map
  59. */
  60. public $defaultZoom = null;
  61.  
  62. /**
  63. * @var bool $enableZoomField If set to boolean true then the zoom value will be assinged to the zoom field
  64. */
  65. public $enableZoomField = null;
  66.  
  67. /**
  68. * @var object $model Object model
  69. */
  70. public $model = null;
  71.  
  72.  
  73. public function init()
  74. {
  75. parent::init();
  76.  
  77. $this->model = ( isset($this->model) ) ? $this->model : null;
  78.  
  79. if ($this->model)
  80. {
  81. $formName = strtolower($this->model->formName());
  82.  
  83. $this->latAttribute = ( isset($this->latAttribute) ) ? $formName.'-'.$this->latAttribute : $formName.'-'.'lat';
  84. $this->lngAttribute = ( isset($this->lngAttribute) ) ? $formName.'-'.$this->lngAttribute : $formName.'-'.'lng';
  85. $this->zoomAttribute = ( isset($this->zoomAttribute) ) ? $formName.'-'.$this->zoomAttribute : $formName.'-'.'zoom';
  86. }
  87. else
  88. {
  89. $this->latAttribute = ( isset($this->latAttribute) ) ? $this->latAttribute : 'lat';
  90. $this->lngAttribute = ( isset($this->lngAttribute) ) ? $this->lngAttribute : 'lng';
  91. $this->zoomAttribute = ( isset($this->zoomAttribute) ) ? $this->zoomAttribute : 'zoom';
  92. }
  93.  
  94. $this->mapCanvasId = ( isset($this->mapCanvasId) ) ? $this->mapCanvasId : 'map';
  95. $this->mapWidth = ( isset($this->mapWidth) ) ? $this->mapWidth : 450;
  96. $this->mapHeight = ( isset($this->mapHeight) ) ? $this->mapHeight : 300;
  97. $this->defaultLat = ( isset($this->defaultLat) ) ? $this->defaultLat : -34.397;
  98. $this->defaultLng = ( isset($this->defaultLng) ) ? $this->defaultLng : 150.644;
  99. $this->defaultZoom = ( isset($this->defaultZoom) ) ? $this->defaultZoom : 8;
  100. $this->enableZoomField = ( isset($this->enableZoomField) ) ? ( ($this->enableZoomField==true) ? 1 : 0 ) : true;
  101.  
  102. $this->registerAssets();
  103.  
  104. }
  105.  
  106. /**
  107. * @inheritdoc
  108. */
  109. public function run()
  110. {
  111. $js = <<<SCRIPT
  112.  
  113. var map = null;
  114. var marker = null;
  115. var markers = [];
  116. var enalbeZoom = $this->enableZoomField;
  117.  
  118. function initMap() {
  119. var mapOptions = {
  120. zoom: $this->defaultZoom,
  121. center: {lat: $this->defaultLat, lng: $this->defaultLng},
  122. mapTypeId: google.maps.MapTypeId.ROADMAP
  123. };
  124.  
  125. map = new google.maps.Map(document.getElementById('$this->mapCanvasId'), mapOptions);
  126.  
  127. google.maps.event.addListener(map, 'click', function(e) {
  128. placeMarker(e.latLng, map);
  129. });
  130.  
  131. google.maps.event.addListener(map, 'zoom_changed', function(e) {
  132. var zoom = map.getZoom();
  133. if (enalbeZoom) { document.getElementById('$this->zoomAttribute').value = zoom; }
  134. });
  135. }
  136.  
  137. google.maps.event.addDomListener(window, 'load', initMap);
  138.  
  139. function placeMarker(position, map) {
  140. if(marker){
  141. marker.setMap(null);
  142. }
  143.  
  144. marker = new google.maps.Marker({
  145. position: position,
  146. draggable: true,
  147. map: map
  148. });
  149.  
  150. var lat = position.lat();
  151. var lng = position.lng();
  152. var zoom = map.getZoom();
  153.  
  154. document.getElementById('$this->latAttribute').value = lat;
  155. document.getElementById('$this->lngAttribute').value = lng;
  156. if (enalbeZoom) { document.getElementById('$this->zoomAttribute').value = zoom; }
  157.  
  158. google.maps.event.addListener(marker, 'drag', function(e) {
  159. var lat = e.latLng.lat();
  160. var lng = e.latLng.lng();
  161. var zoom = map.getZoom();
  162.  
  163. document.getElementById('$this->latAttribute').value = lat;
  164. document.getElementById('$this->lngAttribute').value = lng;
  165. if (enalbeZoom) { document.getElementById('$this->zoomAttribute').value = zoom; }
  166. });
  167.  
  168. map.panTo(position);
  169. /*markers.push(marker);*/
  170. marker.setMap(map);
  171.  
  172. }
  173.  
  174.  
  175. SCRIPT;
  176.  
  177. $this->getView()->registerJs($js);
  178.  
  179. echo '<div id="' . $this->mapCanvasId . '" style="width:'.$this->mapWidth.'px;height:'.$this->mapHeight.'px; margin-bottom: 20px;"></div>';
  180.  
  181. }
  182.  
  183. protected function registerAssets()
  184. {
  185. $view = $this->getView();
  186. $view->registerJsFile('https://maps.googleapis.com/maps/api/js?key=AIzaSyDiP7bkTtT0wDRwh9tqZoYwErHAMc_BgGM', ['position' => View::POS_HEAD]);
  187. }
  188. }
  189.  
  190. <?= $form->field($model, 'fpro_lat') ?>
  191. <?= $form->field($model, 'fpro_lan') ?>
  192. <?= $form->field($model, 'fpro_field2')->textInput(['readOnly' => true]) ?>
  193.  
  194. <?= ibrarturilatlngfinderLatLngFinder::widget([
  195. 'model' => $model,
  196. 'latAttribute' => 'fpro_lat',
  197. 'lngAttribute' => 'fpro_lan',
  198. 'zoomAttribute' => 'fpro_field2',
  199. 'mapCanvasId' => 'map',
  200. 'mapWidth' => 350,
  201. 'mapHeight' => 300,
  202. 'defaultLat' => 13.0915117329,
  203. 'defaultLng' => 77.4077528715,
  204. 'defaultZoom' => 20,
  205. 'enableZoomField' => true,
  206. ]); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement