项目作者: loveorigami

项目描述 :
Google Maps Markers Widget for Yii2
高级语言: JavaScript
项目地址: git://github.com/loveorigami/yii2-gmap.git
创建时间: 2017-02-10T10:44:17Z
项目社区:https://github.com/loveorigami/yii2-gmap

开源协议:MIT License

下载


Google Maps Widgets for Yii2

GoogleMaps Widget displays a set of user addresses as markers on the map.

Installation

The preferred way to install this extension is through composer.

Either run

  1. php composer.phar require loveorigami/yii2-gmap "*"

or add

  1. "loveorigami/yii2-gmap": "*"

to the require section of your composer.json.

Configuration

To configure the Google Maps key or other options like language, version, library, or map options:

  1. echo lo\widgets\gmap\MarkersWidget::widget([
  2. 'googleMapsUrlOptions' => [
  3. 'key' => 'this_is_my_key',
  4. 'language' => 'id',
  5. 'version' => '3.1.18',
  6. ],
  7. 'googleMapsOptions' => [
  8. 'mapTypeId' => 'roadmap',
  9. 'tilt' => 45,
  10. 'zoom' => 5,
  11. ],
  12. ]);

OR via yii params configuration. For example:

  1. 'params' => [
  2. 'googleMapsUrlOptions' => [
  3. 'key' => 'this_is_my_key',
  4. 'language' => 'id',
  5. 'version' => '3.1.18',
  6. ],
  7. 'googleMapsOptions' => [
  8. 'mapTypeId' => 'roadmap',
  9. 'tilt' => 45,
  10. 'zoom' => 10,
  11. ],
  12. ],

To get key, please visit page

Google Maps Options you can find them on the options page

Widgets

* Markers Widget

To use GoogleMaps, you need to configure its [[locations]] property. For example:

  1. echo lo\widgets\gmap\MarkersWidget::widget([
  2. [
  3. 'position' => [$model->lat, $model->lng],
  4. 'open' => true,
  5. 'content' => $model->name,
  6. ],
  7. [
  8. 'position' => [45.143400, -5.372400],
  9. 'content' => 'My Marker',
  10. ]
  11. ]);

* Select Map Location Widget

Declare model class which will save geographic coordinates:

  1. class SearchLocation extends \yii\base\Model
  2. {
  3. ...
  4. public $address;
  5. public $longitude;
  6. public $latitude;
  7. ...
  8. }

Render widget:

  1. $model = new SearchLocation();
  2. $form = \yii\widgets\ActiveForm::begin();
  3. ...
  4. $form->field($model, 'address')->widget(\lo\widgets\gmap\SelectMapLocationWidget::class, [
  5. 'attributeLatitude' => 'latitude',
  6. 'attributeLongitude' => 'longitude',
  7. ]);
  8. ...
  9. \yii\widgets\ActiveForm::end();

To use movable marker on the map describe draggable option:

  1. $model = new SearchLocation();
  2. $form = \yii\widgets\ActiveForm::begin();
  3. ...
  4. $form->field($model, 'address')->widget(\lo\widgets\gmap\SelectMapLocationWidget::className(), [
  5. 'attributeLatitude' => 'latitude',
  6. 'attributeLongitude' => 'longitude',
  7. 'draggable' => true,
  8. ]);
  9. ...
  10. \yii\widgets\ActiveForm::end();

To use custom field template use placeholder {map} for ActiveField:

  1. $model = new SearchLocation();
  2. $form = \yii\widgets\ActiveForm::begin();
  3. ...
  4. $form->field($model, 'address', [
  5. 'template' => '{label}<div class="custom-class"><div class="form-control">{input}</div>{map}</div>{error}',
  6. ])->widget(\lo\widgets\gmap\SelectMapLocationWidget::className(), [
  7. 'attributeLatitude' => 'latitude',
  8. 'attributeLongitude' => 'longitude',
  9. ]);
  10. ...
  11. \yii\widgets\ActiveForm::end();