andrapratama / yii2-select-google-map-location
一个Yii2小部件,用于在地图上选择位置并选择地图坐标
dev-master / 1.0.x-dev
2024-08-03 10:41 UTC
Requires
- php: >=5.4.0
- yiisoft/yii2: 2.*
This package is auto-updated.
Last update: 2024-10-03 11:02:44 UTC
README
一个Yii2小部件,用于在地图上选择位置并选择地图坐标
此扩展向Google地图添加了选择位置的功能。该扩展指明了存储地址、纬度和经度的模型和属性。
选择位置地图时,会切换地图并设置标记到所选位置。记录所选位置的地址和坐标属性。
安装
在控制台运行
php composer.phar require "kalyabin/yii2-select-google-map-location" "*"
注册Google API
首先,按照以下描述注册您的Google API密钥:Google API文档
之后,在Google控制台中启用
- Google地图JavaScript API(记住API密钥)
- Google地点API网络服务
用法
声明将保存地理坐标的模型类
class SearchLocation extends \yii\base\Model { ... public $address; public $longitude; public $latitude; ... }
渲染小部件
$model = new SearchLocation(); $form = \yii\widgets\ActiveForm::begin(); ... $form->field($model, 'address')->widget(\kalyabin\maplocation\SelectMapLocationWidget::className(), [ 'attributeLatitude' => 'latitude', 'attributeLongitude' => 'longitude', 'googleMapApiKey' => '<YOUR_REGISTERED_GOOGLE_MAP_API>', ]); ... \yii\widgets\ActiveForm::end();
要使用可移动标记在地图上,描述draggable选项
$model = new SearchLocation(); $form = \yii\widgets\ActiveForm::begin(); ... $form->field($model, 'address')->widget(\kalyabin\maplocation\SelectMapLocationWidget::className(), [ 'attributeLatitude' => 'latitude', 'attributeLongitude' => 'longitude', 'googleMapApiKey' => '<YOUR_REGISTERED_GOOGLE_MAP_API>', 'draggable' => true, ]); ... \yii\widgets\ActiveForm::end();
要使用自定义字段模板,请使用ActiveField的占位符{map}
$model = new SearchLocation(); $form = \yii\widgets\ActiveForm::begin(); ... $form->field($model, 'address', [ 'template' => '{label}<div class="custom-class"><div class="form-control">{input}</div>{map}</div>{error}', ])->widget(\kalyabin\maplocation\SelectMapLocationWidget::className(), [ 'attributeLatitude' => 'latitude', 'attributeLongitude' => 'longitude', 'googleMapApiKey' => '<YOUR_REGISTERED_GOOGLE_MAP_API>', ]); ... \yii\widgets\ActiveForm::end();