kalyabin/yii2-select-google-map-location

Yii2 widget to select location at map and choose map coordinates

Installs: 14 991

Dependents: 0

Suggesters: 0

Security: 0

Stars: 28

Watchers: 3

Forks: 11

Language:JavaScript

Type:package

1.1.4 2018-06-26 15:20 UTC

This package is auto-updated.

Last update: 2024-03-29 02:47:36 UTC


README

Yii2 widget to select location at map and choose map coordinates

This extension adds functionality to select the location on the Google map. The extension indicates the model and attributes, which stores the address, latitude and longitude.

When choosing a location map of switches and sets the marker to the selected location. The attributes recorded address and coordinates of the selected location.

Latest Stable Version Total Downloads Monthly Downloads Latest Unstable Version License

Install

Run at your console:

php composer.phar require "kalyabin/yii2-select-google-map-location" "*"

Register Google API

First, register your Google API key as described: Google API Documentation

After this, enable at Google console:

  • Google Map JavaScript API (remember API key)
  • Google Places API Web Service

Usage

Declare model class which will save geographic coordinates:

class SearchLocation extends \yii\base\Model
{
    ...
    public $address;
    public $longitude;
    public $latitude;
    ...
}

Render widget:

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

To use movable marker on the map describe draggable option:

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

To use custom field template use placeholder {map} for 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();