Skip to content

Yii2 widget to select location at map and choose map coordinates

License

Notifications You must be signed in to change notification settings

kalyabin/yii2-select-google-map-location

Repository files navigation

yii2-select-map-location

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

About

Yii2 widget to select location at map and choose map coordinates

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •