Jump to content

AjaxRequest and Unisession.sendresponse


JasonReid

Recommended Posts

Hi Farshad.

I've been playing with the google map api, passing data back and forth using the ajax methods and and I'm really beginning to appreciate the power there.

 

I was wondering if i can pass something more complex, like arrays this way ? if so, how do I unwrap it in Delphi/JS .

Or should I be using JSON to pass data around, if so, is there a nice way to wrap/and unwrap the json on either side ?

 

The reason I ask is because at one point i was looking for a way to create a bunch of markers on the map using lat/long values. i thought maybe I could bundle up a bunch of values and pass them back with my response, and then unwarap them client side and run a "addmarker" function for each element of the data. i prefer to keep the JS and delphi seperate and avoid doing things like this (eg) :

 

procedure TMapFRM1.CalcRoute(start_lat,start_long, end_lat,end_long: double);
begin
  if WebMode then
   UniSession.AddJS('if (typeof googleMap=="object")' +
                    '{' +
                    'var directionsDisplay = new google.maps.DirectionsRenderer();' +
	                 'var directionsService = new google.maps.DirectionsService();'    +

                    'directionsDisplay.setMap(googleMap);  ' +

                    'var request = {origin:,destination:,travelMode: google.maps.TravelMode.DRIVING};' +
                    'directionsService.route(request, function(result, status) {' +
   		             ' if (status == google.maps.DirectionsStatus.OK) {' +
     			         '  directionsDisplay.setDirections(result);' +
   		             '  }' +
 		               '});' +
                    '}');
end;

 

 

eventually i ended up writing the AddMarker function in js and then calling it in an iterative loop :

  
procedure TMapFRM1.AddMarker(s_lat, s_long,s_name : ansistring);
begin
 UniSession.AddJS('addMarker('+s_lat+','+s_long+',"' + s_name+'")');
 LBX1TestLBX.Items.Add(s_lat + '-' + s_long);
end;
...
|
|
...

I := UniMainModule.Props.Execute(s_sql,[],@Calls);
   while I.step() do
   begin
     AddMarker(Calls.latitude,
               Calls.longitude,
               Calls.calloid);
   
   end;

in the html I have :

<head>
  <script type="text/javascript">
      function addMarker(lat,long,nme){
        //alert('add a marker ' + nme);
        
        var image = "/files/tools.png";
        
        var latlng = new google.maps.LatLng(lat,long);
        
        var marker = new google.maps.Marker({
     		position: latlng,
     		title:"Call number" + nme,
           id:nme,
           icon:image
 		   });
        
        marker.setMap(googleMap);
        
        google.maps.event.addListener(marker, 'click', function() {
           //alert(marker.id);
           ajaxRequest(MapFRM1.mapHTF1,'showcalldetails',['calloid='+marker.id]);
        });
      }
  </script>

</head>

 

 

this kind of feels right, but I might be missing the point ...

 

Keep up the brilliant work!

Link to comment
Share on other sites

Hi , I really don't know if it is the correct way,

but I use something like that:

 

procedure TMapForm.AddMarker(id: integer; lat, lng, icon, Caption: string; JumpTo: Boolean);
var
 s, m: string;
begin
 //id
 s := IntToStr(id);

 //marker name
 m := 'marker_' + s;

 UniSession.AddJS(
   'var myLatlng = new google.maps.LatLng(' + lat + ',' + lng + ');' +
   'var image = ''' + icon + '''; ' +

   'var ' + m + ' = new google.maps.Marker({ ' +
   'position: myLatlng, ' +
   'map: googleMap, ' +
   'animation: google.maps.Animation.DROP,' +
   'myname: "' + m + '", ' +
   'id: ' + s + ', ' +
   'icon: image, ' +
   'title:"' + Caption + '" ' +
   '}); ' +

   'markersArray.push(' + m + ');' + //Push the marker into array.
   'google.maps.event.addListener(' + m + ', ''click'', function(e) { ' +
   'ajaxRequest(MapForm.EventPanel, ''markerClick'' ,' +
   ' [''marker=' + m + ''', ''lat=''+e.latLng.lat(), ''lng=''+e.latLng.lng()]);}); '
   );

 if JumpTo then
   UniSession.AddJS(
     'googleMap.setZoom(17); ' +
     'googleMap.setCenter(myLatlng); '
     );

end;

 

 

I have also defined in MainForm.Script the marker array.

 

var markersArray = [];

 

When I want to set a series of markers I just call in a loop the AddMarker();

Link to comment
Share on other sites

  • Administrators

Hi Farshad.

I've been playing with the google map api, passing data back and forth using the ajax methods and and I'm really beginning to appreciate the power there.

 

I was wondering if i can pass something more complex, like arrays this way ? if so, how do I unwrap it in Delphi/JS .

 

Any complex structure like objects and arrays must be converted to proper string notation before they are sent to server.

 

JS converts arrays to string using a,b,c,... notation.

Objects must be converted manually.

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...