views:

313

answers:

2

Hello, guys. I`m trying to dynamically represent the changes of coordinates of a groovy domain object via Google Maps API. The object has fields:

Double lat
Double lng

and a method:

 void flyTo(lat, lng){
  Thread.start(){
   while (locked){
    changeCoords (this)
    this.save()
   }
  }

and here is a JS to repopulate changes to the map every 5 seconds:

    <script src="http://maps.google.com/maps?file=api&amp;amp;v=2&amp;amp;key="MY_API_KEY"
            type="text/javascript"></script>
 <script type="text/javascript">
 var usCenterPoint = new GLatLng(39.833333, -98.583333)
 var usZoom = 4
 var map = new GMap2(document.getElementById("map"))

 function load() {
  if (GBrowserIsCompatible()) {
      var latFrom = -900;
      var latTo = -900;
      var lngFrom = -900;
      var lngTo = -900;
      map.setCenter(usCenterPoint, usZoom)
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl()); 
      updateMap();                      
   }
 }

 function updateMap(){
  map.clearOverlays();

  <g:each in="${dronesList}" status="i" var="drone"> 
         var point${drone.id} = new GLatLng(${drone.lat}, ${drone.lng})
         if (latFrom == -900){
           latFrom = ${drone.lat}
           lngFrom = ${drone.lng}
         } else {
    latTo = ${drone.lat}
    lngTo = ${drone.lng}
         }
        var marker${drone.id} = new GMarker(point${drone.id})
        marker${drone.id}.bindInfoWindowHtml("<strong>${drone.name}</strong><br/>${drone.description}<br/>")
          map.addOverlay(marker${drone.id});
         // map.setCenter(point${drone.id},usZoom);
         if (latTo != -900){
          var polyline = new GPolyline([
                                        new GLatLng(latFrom, lngFrom),
                                        new GLatLng(latTo, lngTo)
                                      ], "#ff0000", 10);
           map.addOverlay(polyline);
         }
     </g:each>  
 }
 </script>
  </head>
  <body onload="load(); setInterval('updateMap()', 5000)" onunload="GUnload()">

When I start an execution of a flyTo() method, I'm getting two bugs.

  1. The changes of the object are not populated. In fact, the map does not even show up.
  2. I'm getting an exception in thread when I call a this.save() method: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

How do I resolve those? Is there a better way to do this?

A: 

Changes to your domain model need to happen within the context of a Hibernate session, so when you create a new thread, it looks like it is created outside of a session. Is there a particular reason why you are using Thread.start() ? Also where and when is flyTo() called? I don't I think I noticed a call in your JS to make the update. If you make the flyTo() call within a controller, it will be automatically be bound to a Hibernate session.

nstehr
I want to change lat and lng fields, and represent changes dynamically to the view, so I'm using a Thread.start(). As far as I understand, if I use a simple loop, it will not refresh the page until the loop is finished.flyTo() is called when the button on the form is pressed. And the update is here: <body onload="load(); setInterval('updateMap()', 5000)" onunload="GUnload()">Briefly, I want an object to change it`s coordinates till it reaches certain coords, and represent the actual movement of it on the map.
folone
A: 

Okay, I've got two solution for this one.

First — I should create an action of controller, and cal it through AJAX to update the coordinates. This solution is not really suitable, because I'm actually interested in changing domain-object's state from the outside, not from the view (Hope, I'm explaining clear enough. If not, I'll answer any questions here).

Second — use Quartz plugin.

folone