locked
How to find the last coordinates using javascript RRS feed

  • Question

  • User932259438 posted

    hello,

    my problem: I have my two different coordinate and I want to calculate for different coordinates how much is distance from first and second coordinates.

    My code works, but I don't have any idea how to find the last coordinates with javascript.

    Example:

    Latitude, Longitude
    1,2

    New coordinates (after refresh again, its every second.)

    Need to calculate between 1,2(last coordinates) and new coordinates.

    New coordinate:
    Latitude, Longitude:
    5, 6

    1,2 - 5,6.

    This code works:


    Calculate coordinate:

            function distance(lat1, lon1, lat2, lon2, unit) {
                if ((lat1 == lat2) && (lon1 == lon2)) {
                    return 0;
                }
                else {
                    var radlat1 = Math.PI * lat1 / 180;
                    var radlat2 = Math.PI * lat2 / 180;
                    var theta = lon1 - lon2;
                    var radtheta = Math.PI * theta / 180;
                    var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
                    if (dist > 1) {
                        dist = 1;
                    }
                    dist = Math.acos(dist);
                    dist = dist * 180 / Math.PI;
                    dist = dist * 60 * 1.1515;
                    if (unit == "K") { dist = dist * 1.609344 }
                    if (unit == "N") { dist = dist * 0.8684 }
                    return dist;
                }
            }

    Calling coordinate:

    var dist = distance_on_geoid(1, 2, position.coords.latitude, position.coords.longitude);

    This is my code for calculate all coordinates.

                var i = 0;
                var j = dist;
                if (result === 0)
                    result = Number(i) + Number(j);
                else
                    result = result + j;

    SUM:

    $("#dist").html(result);

    If you need more info about that let me know.

    Thanks for your help.




    For SUM all coordinates












    Wednesday, January 29, 2020 7:42 AM

All replies

  • User1535942433 posted

    Hi progy85,

    As far as I think,you could put coordinates into an array and check if the pointer is more than the array length.

    var Latitude='';
    var Longitude='';
    var xx=[
            {
             "Latitude":"1",
             "Longitude":"2",
            },
            {
             "Latitude":"5",
             "Longitude":"6",
            }
           ];
    $.(each)(xx,function(i,item){
    if(i==item.length-1)
    ....
    })

    Best regards,

    Yijing Sun
     

    Thursday, January 30, 2020 5:36 AM