1. Bogdan Savluk
  2. yii-coordinatepicker

Source

yii-coordinatepicker / CoordinatePicker.php

<?php

/**
 * Widget to use coordinate picker jQuery plugin.
 *
 * Link to original jQuery plugin:
 * @link https://github.com/davidsalazar/coordinate-picker
 *
 * @author Bogdan Savluk <savluk.bogdan@gmail.com>
 */
class CoordinatePicker extends CWidget
{
    /** @var CModel model */
    public $model;

    /** @var string latitude attribute name in $model */
    public $latitudeAttribute = 'latitude';

    /** @var string longitude attribute name in $model */
    public $longitudeAttribute = 'longitude';

    /** @var string latitude input id */
    public $latitudeInputId;

    /** @var string longitude input id */
    public $longitudeInputId;

    /** @var string Label for picker link, when not set “Pick Coordinates” will be used */
    public $label = null;
    /** @var string An absolute URL to the published assets */
    public $assets;

    /**
     *  Publish assets and generate input ids when they is not set
     */
    public function init()
    {
        $this->assets = Yii::app()->getAssetManager()->publish(dirname(__FILE__) . '/CoordinatePicker/coordinate_picker'); //, false, -1, true
        if (!isset($this->latitudeInputId)) {
            $this->latitudeInputId = CHtml::activeId($this->model, $this->latitudeAttribute);
        }
        if (!isset($this->longitudeInputId)) {
            $this->longitudeInputId = CHtml::activeId($this->model, $this->longitudeAttribute);
        }
    }

    /**
     *  Register required scripts and styles, render widget
     */
    public function run()
    {
        $cs = Yii::app()->clientScript;
        $cs->registerCoreScript('jquery');
        $cs->registerScriptFile($this->assets . '/jquery.coordinate_picker.js');
        $cs->registerCssFile($this->assets . '/styles/smodal/shadow.css');

        if (!isset($this->label)) $this->label = 'Pick Coordinates';

        echo CHtml::link($this->label, '#', array('id' => $this->getId()));

        $cs->registerScript($this->getId() . 'script', <<<EOD
$('#{$this->getId()}').coordinate_picker({
	'lat_selector':  '#{$this->latitudeInputId}',
	'long_selector': '#{$this->longitudeInputId}'
});
EOD
        );

    }

}