2011-12-30 69 views
2

我在Yii应用程序中使用egmaps扩展,它是一个非常出色的应用程序。我如何在填充数据库时遇到问题。我的数据库表酒店有两个属性的长和经度。我不是AJAX方面的专家,但我认为ajax正在调用控制器方法。目前我没有任何控制方法,因为我不知道会发生什么样的数据以及如何?egmap谷歌地图Yii没有将数据保存到数据库

到目前为止我的代码信息,但我认为这AJAX并没有叫控制器SaveCoordinates行动

$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); 

回答

1

解决的情况下,其他的可能会被卡住我解决它通过改变POSTGET和URL行

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

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

和控制器代码是

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, 
     )); 
     } 
2

我用POST,它的工作原理。也许在你的模型的规则中没有指定params?