Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jul 2007
    Posts
    61
    Thanks
    10
    Thanked 0 Times in 0 Posts

    How to store a function's return value into an array ??

    Hi,

    I have a function that returns an array object

    like -

    IsWithinGeofence(point)
    {
    ....
    ...

    if(some condition == true)

    {
    return PointsOfInterest[i];
    }

    }


    Here PointsOfInterest[i].lat = 56.45556, PointsOfInterest[i].lng = -2.5413 and i = 2;

    I want to be able to store these three values into three global variables in Javascript in order to use these variables somewhere else on the same page as their source.

    How do I do this ??

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,859
    Thanks
    22
    Thanked 157 Times in 148 Posts
    I want to be able to store these three values into three global variables in Javascript in order to use these variables somewhere else on the same page as their source.
    1) You can declare/initialize the global variables using the var keyword in the global scope (outside of a function), and then pass the variables in by reference to your function IsWithinGeofence():

    Code:
    var myVar1 = 1;  // default value not necessary ( i think... )
    var myVar2 = 2;  // " "
    var myVar3 = 3;  // " "
     
    IsWithinGeofence(point,var1,var2,var3)
    {
         var1 = PointsOfInterest[i].lat;
         var2 = PointsOfInterest[i].lng;
         var3 = i;
    }
     
    IsWithinGeofence(point,myVar1,myVar2,myVar3);
    alert(myVar1 + " " + myVar2 + " " +  myVar3);
    or..

    2) You can add "i" as a property of PointsOfInterest, return the object, and set the value of a new object in the global scope equal to the return value of your function:

    Code:
     
    IsWithinGeofence(point)
    {
       PointsOfInterest[i].i = i;
       return PointsOfInterest[i];
    }
     
    var newObject = IsWithinGeofence(point);
    alert(newObject.lat + " " + newObject.lng + " " +  newObject.i );
    code is untested in both cases, but should work...just giving you the basic idea...

    There's a couple other ways to do this as well, but see if you like those ideas first...
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :-)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!
    ♪♪ …Need Web Hosting For My YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

  • #3
    New Coder
    Join Date
    Jul 2007
    Posts
    61
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Thanks a lot for the reply. I really appreciate it.

    [B]However, on declaring 3 global variables inside the document

    var myVar1 = 1;
    var myVar2 = 2;
    var myVar3 = 3;


    IsWithinGeofence(point,var1,var2,var3)
    {
    var1 = PointsOfInterest[i].lat;
    var2 = PointsOfInterest[i].lng;
    var3 = i;
    }


    IsWithinGeofence(point,myVar1,myVar2,myVar3);

    alert(myVar1 + " " + myVar2 + " " + myVar3);


    On the function call marked in bold, the map hangs up. I am using this in google maps.

    This hangs up in both the methods u showed, as soon as the function IsWithinFixedGeofence() is called.

    On removing the function call, the alert function shows all 3 variables as undefined
    but loads up the map properly


    Here's the URL -

    http://134.36.37.243/login_success.php


    username - carer
    pass - 1234



    Is there a way to be able to assign the variables without calling the function ??

    I would be extremely grateful if you could help out with this..this is the only thing that I am stuck on my project

  • #4
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,859
    Thanks
    22
    Thanked 157 Times in 148 Posts
    First of all, I don;t have any experience with the Google maps API...

    What do you mean the map "hangs up"?

    The URL you gave me is giving me a "null is not an object" error on line 376, likely triggered by this line:

    var lat2 = point.lat()*RADIANS;
    the "point" object is showing up as null in my debugger, meaning that whatever value you are passing in here in the function argument:

    Code:
    IsWithinFixedGeofence(point)
    {
    }

    ...is a null reference...

    The only call to that function is in here:


    // populate data table
    function populateDataTable(text, dataTable) {
    var pointList = [];
    var pushPinList = [];
    if (text != null) {
    var rcd = text.split('\n');
    if (rcd.length >= 1) {
    var ndx = 0;
    var maxLat = -90.0, maxLon = -180.0;
    var minLat = 90.0, minLon = 180.0;
    var numWidth = 25, timeWidth = 160, nameWidth = 100, gpsWidth = 143;
    var spdWidth = 43, headWidth = 43, altWidth = 45;
    var tableData = "";
    tableData += "<table id='data_table' cellspacing='0' cellpadding='0' border='1'>\n";
    tableData += "<thead><tr>";
    tableData += "<th width="+numWidth +" nowrap>" + hdrFont("#") + "</th>";
    tableData += "<th width="+nameWidth+" nowrap>" + hdrFont("Code") + "</th>";
    tableData += "<th width="+timeWidth+" nowrap>" + hdrFont("Date/Time") + "</th>";
    tableData += "<th width="+gpsWidth +" nowrap>" + hdrFont("Lat/Long") + "</th>";
    tableData += "<th width="+spdWidth +" nowrap>" + hdrFont("MPH") + "</th>";
    tableData += "</tr></thead>\n";
    tableData += "<tbody id='data_tbody'>\n";
    var startNdx = rcd.length - MAX_PUSH_PINS - 1; // '- 1' because the last 'rcd' is blank
    if (startNdx < 0) { startNdx = 0; }
    for (var i = startNdx; i < rcd.length; i++) {
    if (rcd[i].trim() != "") {
    var fld = rcd[i].split(',');
    if (fld.length >= 1) {
    var date = (fld.length > 0)? fld[0] : "";
    var time = (fld.length > 1)? fld[1] : "";
    var code = (fld.length > 2)? fld[2] : "";
    var lat = numParseFloat((fld.length > 3)? fld[3] : "0");
    var lon = numParseFloat((fld.length > 4)? fld[4] : "0");
    var kph = numParseFloat((fld.length > 5)? fld[5] : "0");
    var mph = kph * 0.62137; // kph => mph
    if (lat > maxLat) { maxLat = lat; }
    if (lat < minLat) { minLat = lat; }
    if (lon > maxLon) { maxLon = lon; }
    if (lon < minLon) { minLon = lon; }
    ndx++;
    tableData += "<tr>";
    tableData += "<td width="+numWidth +" nowrap>" + dtaFont(ndx) + "</td>";
    tableData += "<td width="+nameWidth+" nowrap>" + dtaFont(code) + "</td>";
    tableData += "<td width="+timeWidth+" nowrap>" + dtaFont(date+" "+time) + "</td>";
    tableData += "<td width="+gpsWidth +" nowrap>" + dtaFont(strParseFloat(lat,5)+"/"+strParseFloat(lon,5)) + "</td>";
    tableData += "<td width="+spdWidth +" nowrap>" + dtaFont(strParseFloat(mph,1)) + "</td>";
    tableData += "</tr>\n";
    var pt = createPoint(lat, lon);

    pointList.push(pt);

    pushPinList.push(createPushPin(pt, ndx, code, mph));


    //Loop through all 6 points of interest to test if the created point lies in the geo-fence


    IsWithinFixedGeofence(pt);

    }
    }
    }
    Trace the value of "pt" in this function backwards until you find the reason why it is a null reference at the time it is included in the IsWithinFixedGeofence function..."pt" variable is devlared on this line of code:

    var pt = createPoint(lat, lon);
    The code for createPoint():

    Code:
     
    // create/return a Google GPoint
    function createPoint(lat, lon) {
    if (window.GPoint) {
    return new GLatLng(lat, lon);
    } else {
    return null;
    }
    }
    obviously at some point, in your for loop inside populateDataTable(), window.GPoint is evaluating to boolean False and thus returning a null value...I don;t know what window.GPoint is -- maybe it's an inherited property from the Google Maps API or script/class library somewhere (again, dont know anything about programmatically manipulating Google Maps)

    Soooo, it looks like you need to change null to a legal object reference (of some sort), or eliminate the conditional statement altogether and just return a value...either way, you need to prevent the value null from being the return value of that function...

    That is my guess as to what the problem is....I'll let you figure out the logic as far as what needs to be done to make it work how you want...good luck!
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :-)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!
    ♪♪ …Need Web Hosting For My YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

  • #5
    New Coder
    Join Date
    Jul 2007
    Posts
    61
    Thanks
    10
    Thanked 0 Times in 0 Posts
    >>What do you mean the map "hangs up"?

    This is a GPS tracking page, if I reload the page again with calling IsWithinFixedGeofence() the way you said, the map showing the GPS location on google map fails to load.


    >>The URL you gave me is giving me a "null is not an object" error on line 376, likely triggered by this line:

    This shows null, because this GPS tracking page cannot find the data.csv on the web server, which is created when the GPS establishes a connection with the server.

    The function populatedatatable reads the points from the data.csv and passes each of these points as "pt" and creates a corresponding marker on the map, to trace where the GPS has traveled.

    This page reads the file from the web server, and then shows each point as a marker on Google maps.


    >>the "point" object is showing up as null in my debugger, meaning that whatever value you are passing in here in the function argument:

    [/SIZE]
    Code:
    IsWithinFixedGeofence(point)
    {
    }
    [SIZE=2]
    ...is a null reference...

    Well again, point is just a formal parameter, the actual parameter that is going inside is point pt when IsWithinFixedGeofence() is called within populatedatatable function



    It's not a problem with the parameter "pt"


    if I use this code


    IsWithinGeofence(point)
    {
    ....
    ...

    if(distance <= minimiumdistance)

    {
    document.write(PointsOfInterest[i].lat);
    }

    }


    It displays the correct value of lat in the case here -56.4856.

    All I want to do is store

    PointsOfInterest[i].lat, PointsOfInterest[i].lng and index i in three global variables from inside the function, which can then be called anywhere else in the file...

    Best Regards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •