antonyz89/yii2-jquery-locationpicker

为yii2的jQuery位置选择器小部件

0.2.5 2018-03-31 16:00 UTC

This package is auto-updated.

Last update: 2024-09-08 20:06:54 UTC


README

该小部件实现了 jquery-locationpicker-plugin

安装

安装此扩展的首选方式是通过 composer

运行

php composer.phar require pigochu/yii2-jquery-locationpicker ">=0.2.0"

或添加

"pigochu/yii2-jquery-locationpicker": ">=0.2.0"

到您的 composer.json 文件的require部分。

测试的基本用法

<?= \pigolab\locationpicker\LocationPickerWidget::widget(); ?>

绑定UI与小部件

此示例是通过 http://logicify.github.io/jquery-locationpicker-plugin/#usage 转换的

<?php
use yii\web\JsExpression;
?>

Location: <input type="text" id="us2-address" style="width: 200px"/>
<br>
Radius: <input type="text" id="us2-radius"/>
<br>
Lat.: <input type="text" id="us2-lat"/>
<br>
Long.: <input type="text" id="us2-lon"/>
<br>


<?php
    echo \pigolab\locationpicker\LocationPickerWidget::widget([
       'key' => 'abcabcabcabc ...',	// require , Put your google map api key
       'options' => [
            'style' => 'width: 100%; height: 400px', // map canvas width and height
        ] ,
        'clientOptions' => [
            'location' => [
                'latitude'  => 46.15242437752303 ,
                'longitude' => 2.7470703125,
            ],
            'radius'    => 300,
            'addressFormat' => 'street_number',
            'inputBinding' => [
                'latitudeInput'     => new JsExpression("$('#us2-lat')"),
                'longitudeInput'    => new JsExpression("$('#us2-lon')"),
                'radiusInput'       => new JsExpression("$('#us2-radius')"),
                'locationNameInput' => new JsExpression("$('#us2-address')")
            ]
        ]        
    ]);
?>

CoordinatesPicker

CoordinatesPicker 允许你在ActiveForm中获取坐标,此外我还实现了原jquery-locationpicker-plugin中没有的一些功能

  • 启用/禁用搜索框,搜索框将覆盖在地图上
  • 启用/禁用所有谷歌地图控件

示例

<?php
	echo $form->field($model, 'coordinates')->widget('\pigolab\locationpicker\CoordinatesPicker' , [
		'key' => 'abcabcabc...' ,	// require , Put your google map api key
		'valueTemplate' => '{latitude},{longitude}' , // Optional , this is default result format
		'options' => [
			'style' => 'width: 100%; height: 400px',  // map canvas width and height
		] ,
		'enableSearchBox' => true , // Optional , default is true
		'searchBoxOptions' => [ // searchBox html attributes
			'style' => 'width: 300px;', // Optional , default width and height defined in css coordinates-picker.css
		],
		'searchBoxPosition' => new JsExpression('google.maps.ControlPosition.TOP_LEFT'), // optional , default is TOP_LEFT
		'mapOptions' => [
			// google map options
			// visit https://developers.google.com/maps/documentation/javascript/controls for other options
            'mapTypeControl' => true, // Enable Map Type Control
            'mapTypeControlOptions' => [
                  'style'    => new JsExpression('google.maps.MapTypeControlStyle.HORIZONTAL_BAR'),
                  'position' => new JsExpression('google.maps.ControlPosition.TOP_LEFT'),
			],
            'streetViewControl' => true, // Enable Street View Control
        ],
		'clientOptions' => [
			// jquery-location-picker options
			'radius'    => 300,
            'addressFormat' => 'street_number',
		]
	]);
?>

获取坐标

默认的valueTemplate是 '{latitude},{longtitude}',因此我们将得到类似 '25.023308046766083,121.46041916878664' 的结果

我们可以通过explode()来转换它

<?php
list($latitude,$longitude) = explode(',' , $model->coordinates);
?>

已弃用的选项:enableMapTypeControl

从版本0.2.0开始,不要使用 'enableMapTypeControl',我添加了 'mapOptions' 来设置谷歌地图选项。你现在可以启用/禁用所有控件或设置控件的样式和位置。

示例:启用rotateControl、streetViewControl、mapTypeControl并设置样式/位置

<?php
   echo $form->field($model, 'coordinates')->widget('\pigolab\locationpicker\CoordinatesPicker' , [

        'clientOptions' => [ 'zoom' => 20 ], // rotateControl will display when zoom is 20
        // .... other options ...
		'mapOptions' => [
			// set google map optinos
			'rotateControl' => true,
			'scaleControl' => false,
			'streetViewControl' => true,
			'mapTypeId' => new JsExpression('google.maps.MapTypeId.SATELLITE'),
			'heading'=> 90,
            'tilt' => 45 ,
                
			'mapTypeControl' => true,
            'mapTypeControlOptions' => [
                  'style'    => new JsExpression('google.maps.MapTypeControlStyle.HORIZONTAL_BAR'),
                  'position' => new JsExpression('google.maps.ControlPosition.TOP_CENTER'),
			]
		]
   ]);
?>

请访问GoogleMaps Full Control文档:https://developers.google.com/maps/documentation/javascript/controls

其他文档