Gmap Multiploting and Capturing all marker updates in Yii
This article will be useful for those who are using jquery-gmap extension in Yii.
Here i am going to tell you how you can plot multiple locations in a single google map using this extension and how you can capture all marker locations when a user changing them.This will be useful when you are giving an option to mark multiple locations for a user and save all the updated locations in a single map.
i hope you already familiar with this extension and you are willing to store a single location through this extension.If you are not you can reffer one of my preveous article with this same extension(Google maps in Yii).
So you might have a location model which stores all the location of a business model.So you will be having ONE-TO-MANY relation from business to location model.
Try the below code in your business create form.(_form.php)
<?php// create a map centered in the middle of the world …
$gmap = new EGmap3Widget();
$gmap->width=’600’;
$gmap->height=’450’;
$gmap->setOptions(array(
‘zoom’ => 14,
‘center’ => array(0,0),
));$model=Business::model()->findbyPk(‘3’);
$i=0;
foreach($model->locations as $loc)
{
//Initialize a marker
$m[$i] = new EGmap3Marker(array(
‘title’ => $loc->shop_name,
‘draggable’=>true,
));
//set location
$m[$i]->latLng=array($loc->latitude,$loc->longitude);/* add dragend event with an autogenerate js function(ie captureMarkerPosition()) */
$m[$i]->addEvent(‘dragend’, ‘function(result){captureMarkerPosition’.$i.’(result);}’);
//add marker to gmap
$gmap->add($m[$i]);/* generate hidden input fields to store locations */
echo “
<input type=’hidden’ name=’Location_id[]’ value=’”.$loc->id.”’ >
<input type=’hidden’ name=’Location_latitude[]’ id=’Location_latitude”.$i.”’ value=’”.$loc->latitude.”’>
<input type=’hidden’ name=’Location_longitude[]’ id=’Location_longitude”.$i.”’ value=’”.$loc->longitude.”’>
”;
/*Generate individual functions to capture the marker position */
echo ‘
<script type=”text/javascript”>
function captureMarkerPosition’.$i.’(marker)
{
document.getElementById(“Location_latitude’.$i.’”).value=marker.getPosition().lat();
document.getElementById(“Location_longitude’.$i.’”).value=marker.getPosition().lng();
}
</script>’;
$i++;}//end loop
?>Now we want to store all these captured positions when the user press the submit button.We can write a some code in the controller action.
<?php
eg:actionCreatLocation.phpif(isset($_POST[‘Location_latitude’])&&isset($_POST[‘Location_latitude’]))
{
$location[‘id’]=$_POST[‘Location_id’];
$location[‘latitude’]=$_POST[‘Location_latitude’];
$location[‘longitude’]=$_POST[‘Location_longitude’];
$i=0;
foreach($location[‘id’] as $id)
{
$l=Location::model()->findbyPk($id);
$l->latitude=$location[‘latitude’][$i];
$l->longitude =$location[‘longitude’][$i];
$l->save(false);
$i++;
}
}
?>
the above code will store the updated marker co-ordinates to Location model.
Regards,
sirin k
Nintriva Wireless
INDIA