Yii Framework Forum: egmap Google Maps Yii not saving data to database - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

egmap Google Maps Yii not saving data to database Rate Topic: -----

#1 User is offline   Afnan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 18-October 11

Posted 30 December 2011 - 10:53 AM

I am using egmaps extensions with Yii application it is a very brilliant application. how ever I am having trouble populating database. my database table hotel has two attributes for long and lat. I am not expert in AJAX but i think that ajax is calling controler method. At present I donot have anything in controler method because I donot know what data will come and how?

my code so far is BUT I THINK THIS AJAX IS NOT CALLING THE CONTROLLER SaveCoordinates action

 $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('hotel/savecoords').'/'.$model->id.'",
                                                        data:({lat: event.latLng.lat(), lng: event.latLng.lng()}),

                                                        cache:false,
                                                    });}', false, EGMapEvent::TYPE_EVENT_DEFAULT);
if($model->long)
{
            // If we have already created marker - show it
                $marker = new EGMapMarker($model->lat, $model->long, array('title' => Yii::t('catalog', $model->name),
                        'icon'=>$icon, 'draggable'=>true), 'marker', array('dragevent'=>$dragevent));
                $marker->addHtmlInfoWindow($info_window_a);
                $gMap->addMarker($marker);
                $gMap->setCenter($model->lat, $model->long);
                $gMap->zoom = 16;
}
else
{
                // 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('hotel/savecoords')."/".$model->id.'",'.
                          '"data":({"lat": event.latLng.lat(), "lng": event.latLng.lng()}),'.
                          '"cache":false,'.
                        '}); }', false, EGMapEvent::TYPE_EVENT_DEFAULT_ONCE));
}
            $gMap->renderMap(array(), Yii::app()->language);

0

#2 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,447
  • Joined: 04-October 10

Posted 30 December 2011 - 11:02 AM

I think your ajax URL is wrong, what is with '/'. $model->id? Shouldn't be like this?

     '"url":"'.$this->createUrl('hotel/savecoords', array('model'=>$model->id)).'",'.


Also, you do not have a success return function set... have you checked the FIREBUG? does it fires an error? If so, then what?

Thanks for using EGMap :)
┬┐How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
1

#3 User is offline   Afnan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 18-October 11

Posted 30 December 2011 - 12:18 PM

View PostAntonio Ramirez, on 30 December 2011 - 11:02 AM, said:

I think your ajax URL is wrong, what is with '/'. $model->id? Shouldn't be like this?

     '"url":"'.$this->createUrl('hotel/savecoords', array('model'=>$model->id)).'",'.


Also, you do not have a success return function set... have you checked the FIREBUG? does it fires an error? If so, then what?

Thanks for using EGMap :)


if i set sucess function then map does not render.

This map control is lovely I wonder how much time developer has taken to develop this:)

ok
1.now how do you check that your ajax is calling in controller?
2.With this map usage it is calling a action in hotel controler. Now what should be written that controller ?
0

#4 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,317
  • Joined: 12-October 09
  • Location:Croatia

Posted 30 December 2011 - 01:08 PM

NOTE: moved to proper section "Extensions" instead of "General Discussion for Yii 1.1.x"
Find more about me.... btw. Do you know your WAN IP?
0

#5 User is offline   Afnan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 18-October 11

Posted 30 December 2011 - 03:54 PM

Hi all. In case if other gets stuck with this problem here is solution

Extension examples did not show the code for controller but i have shown may be if it is correct admins may add it there.But it works like charm in my app


Solved in case other may get stuck I solved it by changing
POST 
to
GET 
and URL Line

url:"'.$this->createUrl('hotel/savecoords').'/'.$model->id.'", '

to

url':'".$this->createUrl('hotel/savecoords', array('id'=>$model->id))."',`

and controller code is
 public function actionSaveCoords($id)
        {
            $model=$this->loadModel($id);

        // Uncomment the following line if AJAX validation is needed
                // 
        // $this->performAjaxValidation($model);
            if(isset ($_GET['lat']))
                $model->lat = $_GET['lat'];
            if(isset ($_GET['lat']))
                $model->long = $_GET['lng'];
            if($model->save())
                {
                    echo 'Thank you for registring your place with '.Yii::app()->name;  
                }
                $this->render('view',array(
            'model'=>$model,
        ));
        }

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users