Git Product home page Git Product logo

yii2-jquery-locationpicker's Introduction

jquery location picker widget for yii2

The widget implement jquery-locationpicker-plugin

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require pigochu/yii2-jquery-locationpicker ">=0.1.2"

or add

"pigochu/yii2-jquery-locationpicker": ">=0.1.2"

to the require section of your composer.json file.

Basic Usage for Test

<?= \pigolab\locationpicker\LocationPickerWidget::widget(); ?>

Binding UI with the widget

This sample is transformed via http://logicify.github.io/jquery-locationpicker-plugin/#usage

<?php
use yii\web\JsExpression;
?>

Location: <input type="text" id="us2-address" style="width: 200px"/>
<br>
Radius: <input type="text" id="us2-radius"/>
<br>
Lat.: <input type="text" id="us2-lat"/>
<br>
Long.: <input type="text" id="us2-lon"/>
<br>


<?php
    echo \pigolab\locationpicker\LocationPickerWidget::widget([
       'key' => 'abcabcabcabc ...',	// optional , Your can also put your google map api key
       'options' => [
            'style' => 'width: 100%; height: 400px', // map canvas width and height
        ] ,
        'clientOptions' => [
            'location' => [
                'latitude'  => 46.15242437752303 ,
                'longitude' => 2.7470703125,
            ],
            'radius'    => 300,
            'inputBinding' => [
                'latitudeInput'     => new JsExpression("$('#us2-lat')"),
                'longitudeInput'    => new JsExpression("$('#us2-lon')"),
                'radiusInput'       => new JsExpression("$('#us2-radius')"),
                'locationNameInput' => new JsExpression("$('#us2-address')")
            ]
        ]        
    ]);
?>

CoordinatesPicker

CoordinatesPicker let you get coordinates in ActiveForm , In addition I implemented some features not in original jquery-locationpicker-plugin :

  • enable/disable search box , search box will overlay on map
  • enable/disable map type control

Example :

<?php
	echo $form->field($model, 'coordinates')->widget('\pigolab\locationpicker\CoordinatesPicker' , [
		'key' => 'abcabcabc...' ,	// optional , Your can also put your google map api key
		'valueTemplate' => '{latitude},{longitude}' , // Optional , this is default result format
		'options' => [
			'style' => 'width: 100%; height: 400px',  // map canvas width and height
		] ,
		'enableSearchBox' => true , // Optional , default is true
		'searchBoxOptions' => [ // searchBox html attributes
			'style' => 'width: 300px;', // Optional , default width and height defined in css coordinates-picker.css
		],
		'enableMapTypeControl' => true , // Optional , default is true
		'clientOptions' => [
			'radius'    => 300,
		]
	]);
?>

Get coordinates :

Default valueTemplate is '{latitude},{longtitude} , So we will get resulit like : '25.023308046766083,121.46041916878664'

We can convert it via explode() :

<?php
list($latitude,$longtitude) = explode(',' , $model->coordinates);
?>

CoordinatesPicker with two fields Model

If your ActiveRecord Model has latitude and longitude and want to let ActiveField auto binding , you can create a temp Model to do this :

TempMode.php

<?php
// TempModel.php
use yii\base\Model;
class TempModel extends Model
    public $coordinates;
    public function rules()
    {
        return [
            [['coordinates'] ,'required'],
        ];
    }
    public function attributeLabels()
    {
        return [
            'coordinates' => 'coordinates',
        ];
    }
?>

In your controller and view , you need do something

  • create TempModel as $model2 then pass to view
  • create hidden fields for latitude and longitude in view.
  • define clientOptions['inputBinding'] of CoordinatesPicker.

Controller's code

public function actionXXX($yourId) {
    $model = YourOriginalModel::findOne($yourId);
    $model2 = new TempModel();
    $model2->coordinates = $model->latitude . "," . $model->longitude;
    return $this->render('XXX' , [
        'model' => $model ,
        'model2' => $model2,
    ]);
}

View's code:

    <?php
		// $model is your original Model
	 	echo $form->field($model, 'latitude')->hiddenInput()->label(false);
	 	echo $form->field($model, 'longitude')->hiddenInput()->label(false);
		// $model2 is TempModel
        echo $form->field($model2, 'coordinates')->widget('\pigolab\locationpicker\CoordinatesPicker' , [
			// ... your other setting
            'clientOptions' => [
                'radius'    => 300,
				'inputBinding' => [
                    'latitudeInput'     => new JsExpression("$('#"  .Html::getInputId($model, "latitude").  "')"),
                    'longitudeInput'    => new JsExpression("$('#"  .Html::getInputId($model, "longitude").  "')"),
				]
            ]
        ])->label('coordinates');

yii2-jquery-locationpicker's People

Contributors

pigochu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.