Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,32 @@ echo $form->field($model, 'coordinates')->widget('kolyunya\yii2\widgets\MapInput
~~~

### Extended example
To configure asset manager use the following configuration

~~~php
return [
// ...
'components' => [
'assetManager' => [
'bundles' => [
\kolyunya\yii2\assets\MapInputAsset::class => [
'options' => [
'key' => 'YOUR_GOOGLE_MAPS_API_KEY',
'language' => 'en',
'libraries' => 'places',
],
],
],
],
],
];
~~~

An exhaustive list of widget parameters (which are not derived from [yii\widgets\InputWidget](http://www.yiiframework.com/doc-2.0/yii-widgets-inputwidget.html)) available for configuration is described in the following example.
~~~php
echo $form->field($model, 'coordinates')->widget(
'kolyunya\yii2\widgets\MapInputWidget',
[

// Google maps browser key.
'key' => $key,

// Initial map center latitude. Used only when the input has no value.
// Otherwise the input value latitude will be used as map center.
// Defaults to 0.
Expand Down
52 changes: 36 additions & 16 deletions sources/assets/MapInputAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,35 @@

namespace kolyunya\yii2\assets;

/**
* Class MapInputAsset
*
* To configure asset manager use the following configuration
*
* return [
* // ...
* 'components' => [
* 'assetManager' => [
* 'bundles' => [
* \kolyunya\yii2\assets\MapInputAsset::class => [
* 'options' => [
* 'key' => 'YOUR_GOOGLE_MAPS_API_KEY',
* 'language' => 'en',
* 'libraries' => 'places',
* ],
* ],
* ],
* ],
* ],
* ];
*
* http://www.yiiframework.com/doc-2.0/guide-structure-assets.html#customizing-asset-bundles
*
* @package kolyunya\yii2\assets
*/

class MapInputAsset extends \yii\web\AssetBundle
{

public static $key;

public $sourcePath = '@kolyunya/yii2-map-input-widget/sources/web';

public $depends =
Expand All @@ -19,26 +43,22 @@ class MapInputAsset extends \yii\web\AssetBundle
'position' => \yii\web\View::POS_END,
];

public function __construct($config = [])
/** @var array */
public $options = [];

/**
* @return void
*/
public function init()
{
$this->js[] = $this->getGoogleMapScriptUrl();
$this->js[] = '//maps.googleapis.com/maps/api/js?' . http_build_query($this->options);

if (YII_DEBUG) {
$this->js[] = 'js/map-input-widget.js';
$this->css[] = 'css/map-input-widget.css';
} else {
$this->js[] = 'js/map-input-widget.min.js';
$this->css[] = 'css/map-input-widget.min.css';
}
parent::__construct($config);
}

private function getGoogleMapScriptUrl()
{
$scriptUrl = "//maps.googleapis.com/maps/api/js?";
$scriptUrl .= http_build_query([
'key' => self::$key,
'libraries' => 'places',
]);
return $scriptUrl;
}
}
12 changes: 0 additions & 12 deletions sources/widgets/MapInputWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

class MapInputWidget extends \yii\widgets\InputWidget
{

public $key;

public $latitude = 0;

public $longitude = 0;
Expand All @@ -31,12 +28,8 @@ class MapInputWidget extends \yii\widgets\InputWidget

public function run()
{

Yii::setAlias('@kolyunya','@vendor/kolyunya');

// Asset bundle should be configured with the application key
$this->configureAssetBundle();

return $this->render(
'MapInputWidget',
[
Expand All @@ -56,9 +49,4 @@ public function run()
]
);
}

private function configureAssetBundle()
{
\kolyunya\yii2\assets\MapInputAsset::$key = $this->key;
}
}