As promised: http://bit.ly/fHPdvK
Antonio, very very buen extension!
But… addListenerOnce? Force UI language? I’m afraid i’ll have to make those manually, which is sad.
Can you take me to an example? I don’t really understood… thanks!
You know that there is a parameter array on renderMap, that you can include whatever javascript code you wish?
/**
*
* Lazy Programmer's function to register the javascript needed and display HTML
* map container
* @param array $afterInit -javascript code to be rendered after init call
* @param string $language -preferred language setting for the results
* @param string $region -top level geographic domain
* @param ClientScript::CONSTANT $position -where to render the script
*/
In the addListenerOnce I guess it would be:
$gMap = new EGMap();
....
// other code
....
// render our addListenerOnce
array('google.maps.addListenerOnce('.$gMap->getJsName().',"click",function(event){ alert("event");});');
// will render this
// just thinking that 'map' is the jsName, as calling getJsName() will return
// custom name or automatically generated name
google.maps.addListenerOnce(map, "click", function(event) {
alert("event");
});
So, not really sure you have to do what you request manually. I know the documentation is poor, but I try to change that bit.
Cheers
Uhm i cannot add links here, as never posted on forum before Look for addListenerOnce in GMaps API V3.
apis/maps/documentation/javascript/reference.html#MapsEventListener
And for language, the basic language control (hl) doesn’t work with your extension. You have if($language != null) on line 463 in EGMap.php, which does nothing. You should bind language variable into $params array.
Just realized that language support was given on function parameters and didn’t include it on api parameters
Thanks for pointing that out Johnattan, I will change that asap
As for addListenerOnce, i was expecting to write something like
$event = new EGMapEvent('click', 'myjavascriptfunc()', false, EGMapEvent::TYPE_EVENT_DEFAULT_ONCE);
and get
google.maps.addListenerOnce('map',"click",myjavvascriptfunc());
I’ve done it already, but had to modify your code which is bad. Thinking to make and extension of EGMapEvent.
Also, documentation of EGMapEvent lacks info about $type argument for object creation.
Hey Johnatan, modifying my code is not bad as long as it increases its performance and adapts new things to the library. You should upload the patch here and I will update the library. I am actually thinking to submit this extension to a Git Repository so all of you guys can add new functionality to it.
I wait for your modification to update the library with ion (regions) and languages support (i included and forgot about them)
Thanks again
Tbh i have no idea how to upload a patch here. I suppose i have to read the “newbie” section. I bet if you have a look into EGMapEvent.php, you’ll easily see what has to be changed to add the additional “once” types of event.
I’m sure it will be faster than me trying to upload the patch right now. Sorry for inconvenience.
Hi, just write a normal post and use the ‘< >’ on the HTML appeared editor to paste code. Or just paste the code here.
<?php
/**
*
* EGMapEvent
*
* Modified by Antonio Ramirez
* @since 2011-01-22
* @link http://www.ramirezcobos.com
*
* changeLog: 2011-01-22 Included toJs for custom event type selection (DOM or DEFAULT)
*
* A googleMap Event
* @author Fabrice Bernhard
*
*
* @copyright
* info as this library is a modified version of Fabrice Bernhard
*
* Copyright (c) 2008 Fabrice Bernhard
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
class EGMapEvent
{
const TYPE_EVENT_DEFAULT = 'DEFAULT';
const TYPE_EVENT_DEFAULT_ONCE = 'DEFAULT_ONCE';
const TYPE_EVENT_DOM = 'DOM';
const TYPE_EVENT_DOM_ONCE = 'DOM_ONCE';
protected $trigger;
protected $function;
protected $encapsulate_function;
protected $type = self::TYPE_EVENT_DEFAULT;
/**
* @param string $trigger action that will trigger the event
* @param string $function the javascript function to be executed
* @param string $encapsulate_function
* @author Fabrice Bernhard
*/
public function __construct($trigger,$function,$encapsulate_function=true, $type=self::TYPE_EVENT_DEFAULT)
{
$this->trigger = $trigger;
$this->function = $function;
$this->encapsulate_function = $encapsulate_function;
$this->setType($type);
}
/**
*
* Sets the type of event, by default Google Event
* @param string $type
* @throws CException
*/
public function setType( $type ){
if( $type !== self::TYPE_EVENT_DEFAULT && $type !== self::TYPE_EVENT_DEFAULT_ONCE &&
$type !== self::TYPE_EVENT_DOM && $type !== self::TYPE_EVENT_DOM_ONCE )
throw new CException( Yii::t('EGMap', 'Unrecognized Event type') );
$this->type = $type;
}
/**
*
* Returns type of event
* @return string
*/
public function getType( ){
return $this->type;
}
/**
* @return string $trigger action that will trigger the event
*/
public function getTrigger()
{
return $this->trigger;
}
/**
* @return string $function the javascript function to be executed
*/
public function getFunction()
{
if (!$this->encapsulate_function)
return $this->function;
else
return 'function() {'.$this->function.'}';
}
/**
* returns the javascript code for attaching a Google event to a javascript_object
*
* @param string $js_object_name
* @return string
* @author Fabrice Bernhard
*/
public function getEventJs($js_object_name, $once=false)
{
$once = ($once)?'Once':'';
return 'google.maps.event.addListener'.$once.'('.$js_object_name.', "'.$this->getTrigger().'", '.$this->getFunction().');'.PHP_EOL;
}
/**
* returns the javascript code for attaching a dom event to a javascript_object
*
* @param string $js_object_name
* @return string
* @author Fabrice Bernhard
*/
public function getDomEventJs($js_object_name, $once=false)
{
$once = ($once)?'Once':'';
return 'google.maps.event.addDomListener'.$once.'('.$js_object_name.', "'.$this->getTrigger().'", '.$this->getFunction().');'.PHP_EOL;
}
/**
* returns the javascript code for attaching a Google event or a dom event to a javascript_object
*
* @param string $js_object_name
* @return string of event type
* @author Antonio Ramirez
*/
public function toJs( $js_object_name ){
switch ($this->type) {
case self::TYPE_EVENT_DEFAULT_ONCE:
return $this->getEventJs($js_object_name, true);
case self::TYPE_EVENT_DOM:
return $this->getDomEventJs($js_object_name);
case self::TYPE_EVENT_DOM_ONCE:
return $this->getDomEventJs($js_object_name, true);
case self::TYPE_EVENT_DEFAULT:
default:
return $this->getEventJs($js_object_name);
}
}
}
Also added $this->setType($type); line in __construct, as it lacked the type change.
You forgot to put a mention of yourself… thanks johnatan
How to do that using egmaps??
like : http://gmaps-samples-v3.googlecode.com/svn/trunk/sidebar/random-markers.html
Here you have more complex example of coding with EGMap 2.0 where $map is model of database table to save coordinates of a marker point.
Yii::import('ext.gmaps.*');
$gMap = new EGMap();
$gMap->setWidth(880);
$gMap->setHeight(550);
$gMap->zoom = 6;
$mapTypeControlOptions = array(
'position' => EGMapControlPosition::RIGHT_TOP,
'style' => EGMap::MAPTYPECONTROL_STYLE_HORIZONTAL_BAR
);
$gMap->mapTypeId = EGMap::TYPE_ROADMAP;
$gMap->mapTypeControlOptions = $mapTypeControlOptions;
// Preparing InfoWindow with information about our marker.
$info_window_a = new EGMapInfoWindow("<div class='gmaps-label' style='color: #000;'>Hi! I'm your marker!</div>");
// Setting up an icon for marker.
$icon = new EGMapMarkerImage("http://google-maps-icons.googlecode.com/files/car.png");
$icon->setSize(32, 37);
$icon->setAnchor(16, 16.5);
$icon->setOrigin(0, 0);
// Saving coordinates after user dragged our marker.
$dragevent = new EGMapEvent('dragend', "function (event) { $.ajax({
'type':'POST',
'url':'".$this->createUrl('catalog/savecoords').'/'.$items->id."',
'data'<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/sad.gif' class='bbc_emoticon' alt=':(' />{'lat': event.latLng.lat(), 'lng': event.latLng.lng()}),
'cache':false,
});}", false, EGMapEvent::TYPE_EVENT_DEFAULT);
// If we have already created marker - show it
if ($map) {
$marker = new EGMapMarker($map->lat, $map->lng, array('title' => Yii::t('catalog', $items->type->name),
'icon'=>$icon, 'draggable'=>true), 'marker', array('dragevent'=>$dragevent));
$marker->addHtmlInfoWindow($info_window_a);
$gMap->addMarker($marker);
$gMap->setCenter($map->lat, $map->lng);
$gMap->zoom = 16;
// If we don't have marker in database - make sure user can create one
} else {
$gMap->setCenter(38.348850, -0.477551);
// Setting up new event for user click on map, so marker will be created on place and respectful event added.
$gMap->addEvent(new EGMapEvent('click',
'function (event) {var marker = new google.maps.Marker({position: event.latLng, map: '.$gMap->getJsName().
', draggable: true, icon: '.$icon->toJs().'}); '.$gMap->getJsName().
'.setCenter(event.latLng); var dragevent = '.$dragevent->toJs('marker').
'; $.ajax({'.
'"type":"POST",'.
'"url":"'.$this->createUrl('catalog/savecoords')."/".$items->id.'",'.
'"data"<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/sad.gif' class='bbc_emoticon' alt=':(' />{"lat": event.latLng.lat(), "lng": event.latLng.lng()}),'.
'"cache":false,'.
'}); }', false, EGMapEvent::TYPE_EVENT_DEFAULT_ONCE));
}
$gMap->renderMap(array(), Yii::app()->language);
I will write an article about how to do that with EGMap. By the way Johnatan, I am very happy to see that the extension is so useful. Thanks for the code. I will upload asap your modification…
Cheers
Thanks,
I’ll wait
No need to wait anymore, here the link http://bit.ly/gMjmj2
EGMap 2.0 is quite flexible… I guess a better wiki should be provided.
Thanks
I have uploaded the extension with the Event Modifications by Johnatan.
Thanks,
I already test your code and it works great…
I’ll use it
Happy it worked out for you!
Cheers