kalayabin / yii2-select-google-map-location

Yii2 小部件,用于在地图上选择位置并选择地图坐标

安装次数: 15,910

依赖项: 0

建议者: 0

安全性: 0

星标: 28

关注者: 3

分支: 13

语言:JavaScript

类型:

1.1.4 2018-06-26 15:20 UTC

This package is auto-updated.

Last update: 2024-08-29 03:53:42 UTC


README

Yii2 小部件,用于在地图上选择位置并选择地图坐标

此扩展向 Google 地图添加了选择位置的功能。扩展指示模型和属性,用于存储地址、纬度和经度。

选择位置地图时,开关会切换并设置标记到所选位置。记录的属性包括所选位置的地址和坐标。

Latest Stable Version Total Downloads Monthly Downloads Latest Unstable Version License

安装

在控制台运行

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();

要使用可移动标记在地图上,描述可拖动选项

$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();

要使用自定义字段模板,请使用 {map} 占位符替换 ActiveField

$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();