$value)
$this->{$name} = $value;
}
public function render()
{
$this->registerScripts();
$this->renderMap();
if ($this->popupOptions !== null)
{
$this->renderPopupMap();
}
}
protected function renderMap()
{
$gMap = new EGMap();
$gMap->setJsName('geomap');
$gMap->width = $this->width;
$gMap->height = $this->height;
$gMap->zoom = $this->zoom;
$gMap->setCenter($this->latitude, $this->longitude);
$gMap->addGlobalVariable('geocoder');
$gMap->addGlobalVariable('geomap', 'null');
if ($this->popupOptions === null)
{
$gMap->addGlobalVariable('hgeomap', 'null');
$gMap->addGlobalVariable('hgmarker', 'null');
}
$gMap->addEvent(new EGMapEvent('click', 'function(e){panToMap(geomap,e.latLng,hgeomap);}', false));
$gMap->appendMapTo('#geomap');
$afterInit = array(
'geocoder = new google.maps.Geocoder();',
'gmarker = new google.maps.Marker({
position: geomap.getCenter(),
map: geomap,
draggable: true
});',
'google.maps.event.addListener(gmarker, "dragend", function(e) {
panToMap(geomap,e.latLng,hgeomap);
});'
);
echo CHtml::openTag('div', array( 'style' => 'width:' . $this->width));
if($this->searchBox)
{
$this->renderAddressSearchBox ('geoaddress');
$afterInit[] = 'qDefaultText.init({"geoaddress": "Enter address here"});';
}
if($this->geocodeOnShow)
$afterInit[] = 'panToMap(geomap,geomap.getCenter(),hgeomap)';
$gMap->renderMap($afterInit);
echo '
';
echo CHtml::openTag('ul', array('class' => 'message', 'style' => 'margin-bottom:-2px;margin-right:-2px'));
echo 'Drag the marker or click the map to specify new address.';
echo CHtml::closeTag('ul');
echo '';
if ($this->popupOptions !== null)
echo 'Open Big Map';
echo CHtml::closeTag('div');
}
protected function renderPopupMap()
{
$gMap = new EGMap();
$gMap->setJsName('hgeomap');
$gMap->addGlobalVariable('hgmarker');
$gMap->width = isset($this->popupOptions['mapWidth']) ? $this->popupOptions['mapWidth'] : '100%';
$gMap->height = isset($this->popupOptions['mapHeight']) ? $this->popupOptions['mapHeight'] : '600px';
$gMap->zoom = 6;
$gMap->setCenter($this->latitude, $this->longitude);
$gMap->addEvent(new EGMapEvent('click', 'function(e){panToMap(geomap,e.latLng,hgeomap);}', false));
$gMap->appendMapTo('#popup-map');
$this->popupOptions['width'] = isset($this->popupOptions['width']) ? $this->popupOptions['width'] : '800px';
$afterInit = array(
'hgmarker = new google.maps.Marker({
position: hgeomap.getCenter(),
map: hgeomap,
draggable: true
});',
'google.maps.event.addListener(hgmarker, "dragend", function(e) {
panToMap(geomap,e.latLng,hgeomap);
});',
'$("#open-big-map").click(function(e){
e.preventDefault();
$("#hgeomap").dialog({resizable:false,title:"Location",width:"'.$this->popupOptions['width'].'"});
google.maps.event.trigger(hgeomap, "resize");
hgeomap.setCenter(geomap.getCenter());
return false;
});'
);
echo CHtml::openTag('div', array('id'=>'hgeomap','style' => 'display:none'));
if(isset($this->popupOptions['searchBox']) && $this->popupOptions['searchBox'])
$this->renderAddressSearchBox ('hgeoaddress');
if(!is_array($this->afterInitEvents))
$this->afterInitEvents = array($this->afterInitEvents);
$gMap->renderMap(array_merge($this->afterInitEvents,$afterInit));
echo '';
echo CHtml::closeTag('div');
}
protected function renderAddressSearchBox($id)
{
echo '';
echo CHtml::textField($id, '', array('class'=>'input-2','id'=>$id));
echo CHtml::link('Go to','#', array('class'=>'btn-medium', 'onclick'=>'return geocode("'.$id.'");'));
echo '
';
echo '';
}
protected function registerScripts()
{
$updateFieldsJS='';
$fields=array();
if($this->latId !== null )
$updateFieldsJS.="$('#{$this->latId}').val(ll.lat());";
if($this->lngId !== null)
$updateFieldsJS.="$('#{$this->lngId}').val(ll.lng());";
$updateFieldsJS.="reverseGeocode();";
$reverseJS='';
if($this->addressId !== null){
$reverseJS.="$('#{$this->addressId}').val(geoValue('route'));";
$fields[] = '#'.$this->addressId;
}
if($this->zipId !== null){
$reverseJS.="$('#{$this->zipId}').val(geoValue('postal_code'));";
$fields[] = '#'.$this->zipId;
}
if($this->cityId !== null)
{
$reverseJS.="var city = geoValue('locality');";
$reverseJS.="if(city=='') city = geoValue('sublocality');";
$reverseJS.="$('#{$this->cityId}').val(city);";
$fields[] = '#'.$this->cityId;
}
if($this->regionId !== null)
{
$reverseJS.="$('#{$this->regionId}').val(geoValue('administrative_area_level_1'));";
$fields[] = '#'.$this->regionId;
}
if($this->countryId !== null)
{
$reverseJS.="var country = geoValue('country');";
$reverseJS.="$('#{$this->countryId}').val(country);";
$fields[] = '#'.$this->countryId;
}
if($this->formId !== null)
{
$reverseJS.=<<formId}');
if(form.length && $.fn.yiiactiveform){
var settings = form.data('settings');
$.each(settings.attributes, function(){this.status = 3;});
$.fn.yiiactiveform.validate(form,function(data){
$.each(settings.attributes, function(i, attribute){
$.fn.yiiactiveform.updateInput(attribute, data, form);
});
});
}
EOJS;
}
$fieldsStr = implode(',', $fields);
$fieldsJS =
"$('#{$this->latId}, #{$this->lngId}').change(function(){
var latLng = new google.maps.LatLng($('#{$this->latId}').val(), $('#{$this->lngId}').val());
panToMap(geomap,latLng,hgeomap);
});
//var fields = ['".implode("','", $fields)."'];
//$('{$fieldsStr}').change(function(){
// $('#{$this->latId}, #{$this->lngId}').val('');
//});
";
$js = << 0){
if(geomap){
geomap.fitBounds(results[0].geometry.viewport);
gmarker.setPosition(geomap.getCenter());
updateFields(geomap.getCenter());
}
if(hgeomap){
hgeomap.fitBounds(results[0].geometry.viewport);
hgmarker.setPosition(geomap.getCenter());
}
reverseGeocodeResult(results, status);
}else
qAlert("Geocode was not successful for the following reason: " + status);
}
function geoValue(type){
var i, j, result, types, results;
results = currentReverseGeocodeResponse[0].address_components;
// Loop through the Geocoder result set. Note that the results
// array will change as this loop can self iterate.
for (i = 0; i < results.length; i++) {
result = results[i];
types = result.types;
for (j = 0; j < types.length; j++) {
if (types[j] === type)
return result.long_name || '';
}
}
return '';
}
EOD;
$cs = Yii::app()->clientScript;
$cs->registerScript('EGMapGeocodeToolJS', $js, CClientScript::POS_END);
if($this->latId && $this->lngId)
$cs->registerScript('EGeocodeToolJSor',$fieldsJS, CClientScript::POS_READY);
}
}
?>