开发者

Grails + Google Maps API

开发者 https://www.devze.com 2022-12-19 03:19 出处:网络
I`m trying to dynamically represent the changes of coordinates of a groovy domain object via Google Maps API. The object has fields:

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;v=2&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 = ${dr开发者_运维百科one.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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号