public function onAfterElementSave()
 {
     $handle = $this->model->handle;
     $data = $this->element->{$handle};
     $location = GoogleMaps_LocationRecord::model()->findByPk($data->locationId ?: 0);
     if (!$location) {
         $location = new GoogleMaps_LocationRecord();
         $location->elementId = $this->element->id;
         $location->handle = $handle;
     }
     $response = $data->getResponse();
     $location->address = $response->formatted_address;
     $location->addressComponents = $response->address_components;
     $location->title = $this->element->title;
     $location->content = implode('<br>', explode(',', $response->formatted_address));
     $location->lat = $data->latitude;
     $location->lng = $data->longitude;
     $location->save();
     $data->locationId = $location->id;
     $this->element->getContent()->setAttribute($handle, $data->toJson());
     craft()->content->saveContent($this->element);
 }
 public function onAfterElementSave()
 {
     $handle = $this->model->handle;
     $data = $this->element->{$handle};
     foreach ($data->markers as $index => $marker) {
         if ($marker->deleted) {
             if (isset($marker->locationId)) {
                 $location = GoogleMaps_LocationRecord::model()->findByPk($marker->locationId);
                 if ($location) {
                     $location->delete();
                 }
             }
             $data->removeMarker($index);
         } else {
             $marker->isNew = false;
             $marker->elementId = $this->element->id;
             $location = GoogleMaps_LocationRecord::model()->findByPk(isset($marker->locationId) ? $marker->locationId : 0);
             if (!$location) {
                 $location = new GoogleMaps_LocationRecord();
                 $location->elementId = $this->element->id;
                 $location->handle = $handle;
             }
             $location->address = $marker->address;
             $location->addressComponents = $marker->addressComponents;
             $location->title = $marker->title;
             $location->content = $marker->content;
             $location->lat = $marker->lat;
             $location->lng = $marker->lng;
             $location->save();
             $marker->locationId = $location->id;
         }
     }
     foreach ($data->polygons as $index => $polygon) {
         if ($polygon->deleted) {
             $data->removePolygon($index);
         } else {
             $polygon->elementId = $this->element->id;
             $polygon->isNew = false;
         }
     }
     foreach ($data->polylines as $index => $polyline) {
         if ($polyline->deleted) {
             $data->removePolyline($index);
         } else {
             $polyline->elementId = $this->element->id;
             $polyline->isNew = false;
         }
     }
     foreach ($data->routes as $index => $route) {
         if ($route->deleted) {
             $data->removeRoute($index);
         } else {
             $route->elementId = $this->element->id;
             $route->isNew = false;
         }
     }
     foreach ($data->circles as $index => $circle) {
         if ($circle->deleted) {
             $data->removeCircle($index);
         } else {
             $circle->elementId = $this->element->id;
             $circle->isNew = false;
         }
     }
     foreach ($data->groundOverlays as $index => $overlay) {
         if ($overlay->deleted) {
             $data->removeGroundOverlay($index);
         } else {
             $overlay->elementId = $this->element->id;
             $overlay->isNew = false;
         }
     }
     if (isset($this->element->{$handle})) {
         $this->element->getContent()->{$handle} = $data->toJson();
         craft()->content->saveContent($this->element);
     }
     parent::onAfterElementSave();
 }