https://github.com/sergmoro1/yaMap
/extensions/yamap/yaMap.php
Код: Выделить всё
<?php
/**
* yaMap
*
* @author [email protected]
*
*/
class yaMap extends CWidget
{
public $visible = true;
public $points = '';
public $params = array('zoom'=>13,'width'=>'420px','height'=>'210px');
public function init()
{
// Don't do anything if I'm not visible
if(!$this->visible) {
return;
}
// If we have no points, just don't do anything
if(count($this->points) == 0 || $this->points=='') {
$this->visible = false;
return;
}
Yii::app()->clientScript->registerScript(0,
'yaMapPoints='.CJSON::encode($this->points).';'.
'yaMapParams='.CJSON::encode($this->params).';',
CClientScript::POS_READY
);
$this->publishAssets();
parent::init();
}
public function run()
{
$this->render('yaMap');
}
public function publishAssets()
{
$assets = dirname(__FILE__).'/assets';
$baseUrl = Yii::app()->assetManager->publish($assets);
if(is_dir($assets))
{
Yii::app()->clientScript->registerCoreScript('jquery');
Yii::app()->clientScript->registerScriptFile('http://api-maps.yandex.ru/2.0-stable/?load=package.standard&lang=ru-RU');
Yii::app()->clientScript->registerScriptFile($baseUrl . '/yamap.js', CClientScript::POS_HEAD);
} else
{
throw new Exception('yaMap - Error: Couldn\'t find assets to publish.');
}
}
}
/extensions/yamap/views/yaMap.php
Код: Выделить всё
<div id='map_canvas' style='width: 420px; height: 210px; display:none;'></div>
/extensions/yamap/assets/yamap.js
Код: Выделить всё
ymaps.ready(init);
var map;
function init() {
if(yaMapPoints.length != 0) {
var point=yaMapPoints[0];
var mc=document.getElementById('map_canvas');
var display=mc.style.display;
mc.style.display='block';
mc.style.width=yaMapParams['width'];
mc.style.height=yaMapParams['height'];
map = new ymaps.Map ('map_canvas', {center: [point['lat'], point['lng']], zoom: yaMapParams['zoom']});
map.controls.add('smallZoomControl');
map.controls.add('mapTools');
var collection = new ymaps.GeoObjectCollection();
for(var i=0;i<yaMapPoints.length;i++) {
point=yaMapPoints[i];
collection.add(makePlacemark(point));
}
map.geoObjects.add(collection);
if(display=='none') mc.style.display='none';
}
}
function makePlacemark(point) {
newPlacemark = new ymaps.Placemark([point['lat'], point['lng']], {
// Свойства
iconContent: point['icon'],
balloonContentHeader: point['header'],
balloonContentBody: '<em>' + point['body'] + '</em>',
balloonContentFooter: point['footer'] }, {
// Опции
preset: 'twirl#blueStretchyIcon' // иконка растягивается под контент
});
return newPlacemark;
}
Во view
Код: Выделить всё
...
<tr>
<th class='bottom'>
Адрес
</td>
<td class='bottom'>
<?php echo $model->street; ?>
<?php echo $model->townregion; ?>
<?php if($model->lat<>0): ?>
<span class='dotted-link' style='margin-left:10px;' onclick="
var map_canvas=document.getElementById('map_canvas');
if(map_canvas.style.display=='none')
{
map_canvas.style.display='block';
this.innerHTML='скрыть карту';
} else
{
map_canvas.style.display='none';
this.innerHTML='показать на карте';
}
">показать на карте</span>
<?php else: ?>
<span style='margin-left:10px;'>не найден на карте</span>
<?php endif; ?>
</td>
</tr>
<tr><td colspan=2>
<?php
if($model->lat<>0)
{
$this->widget('ext.yaMap.YaMap',
array(
'points' => array(
array(
'lat' => $model->lat,
'lng' => $model->lng,
'icon' => round($model->getPrice()/1000000),
'header' => $model->getPrice(),
'body'=> $model->title,
'footer' => $model->firm->firmContacts[0]->phone
),
)
)
);
}
?>
</td></tr>
...
Код: Выделить всё
...
<?php
$i=1; $points=array();
foreach($model->firmContacts as $contact):
$points[]=array(
'lat' => $contact->lat,
'lng' => $contact->lng,
'icon' => $i++,
'header' => $model->firmIssue->short_name,
'body'=> $contact->address,
'footer' => $contact->phone
);
?>
...
<?php
$this->widget('ext.yaMap.YaMap',
array(
'points' => $points,
'params' => array('zoom'=>11),
));
?>
...