Data communication between MFC and JS for Google Maps application

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ursular
    New Member
    • May 2010
    • 3

    #1

    Data communication between MFC and JS for Google Maps application

    Hi all,

    I'm new to JavaScript and Google Maps API. I got some problems but I cannot find out the reason. Hope someone can help me.

    What I tried to do:
    I'm trying to develop an application in MFC (Visual Studio 2005) with a html dialog showing a Google map. I will send location data from the MFC application to a JS function to calculate directions. Another JS function will return the number of routes and steps in the GDirection object. The result will be sent back as a message string.

    The problem I encountered:
    If I open the html file using IE and manually input the location data and use alert function to show the message, everything works well. I can send the location data from MFC to JS. However, after that, I cannot get the route number back... It always returns the number as "undefined" .


    Some codes:

    Code:
        function initialize() {
          if (GBrowserIsCompatible()) {      
            map = new GMap2(document.getElementById("map_canvas"));
            gdir = new GDirections(map, document.getElementById("directions"));
            GEvent.addListener(gdir, "load", onGDirectionsLoad);
            GEvent.addListener(gdir, "error", handleErrors);
          }
        }
    
        function UpdateGPS(ar) // this is where MFC send the location data to JS
        {
          var a = new VBArray(ar);
          var b = a.toArray();
          rExp = /(\w|\s)*[^@]/ig;
          var res = b[0].match(rExp);	// Name @ size @ free_space
          lan = parseInt(res[1]) / 1000000;
          lon = parseInt(res[2]) / 1000000;	
          str = lan + "," + lon;		
          setDirections(str, "1.300983,103.772704");
        } 
    	
        function getRoute() // this is where JS should send information back to MFC
        {
          return message;
        }
    
        function setDirections(fromAddress, toAddress) 
        {
          gdir.load("from: " + fromAddress + " to: " + toAddress, {getPolyline:true, getSteps:true, travelMode:G_TRAVEL_MODE_WALKING});
        }
    
        function onGDirectionsLoad()
        {
          n_route = gdir.getNumRoutes();
          message = str + ". we have " + n_route + " route.";	
          alert(message);
        }
    Can someone help me here? Thanks a lot!
    Last edited by gits; May 14 '10, 11:49 AM. Reason: added code tags
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    so you call the getRoute()-method? what does an alert in this function say about the variable message? i assume you have it declared globally before? is there anything that could drop the value after loading the data? are you sure that getRoute() is called after! the request is ready?

    Comment

    • ursular
      New Member
      • May 2010
      • 3

      #3
      Hi gits, thank you very much for your reply.

      "so you call the getRoute()-method? what does an alert in this function say about the variable message? i assume you have it declared globally before? is there anything that could drop the value after loading the data? are you sure that getRoute() is called after! the request is ready? "

      Yes I call the getRoute() function in MFC. The second part of string I got in MFC is: we have undefined route. Yes I'm sure the getRoute() function is called because the first part of the string I got shows the lan and lon which I sent from MFC to UpdateGPS() function. I declared n_route as a globan variable, yes...

      I guess there are two reasons, but I don't know how to check them... Firstly as you said the value dropped... but how could I know whether the value dropped somewhere? I don't know how to debug my MFC application with the html file together. Do you know how to do that?

      Secondly, I think maybe it is because the gdir.load() function has not finished when I called the getRoute() function, but I added delay function before I called the getRoute() function.

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        i would say that you should go for your second thought ... a delay isn't reliable ... you might set a variable to poll or poll the getRoute() until the value is not 'undefined' anymore ...

        Comment

        • ursular
          New Member
          • May 2010
          • 3

          #5
          Hi gits,

          Thanks... I shall try that tomorrow and let you know :D

          Comment

          Working...