Loop structure for adding up coordinates in an array - javascript

I have an array with coordinates in and want to add them up to determine a total distance (not just from point A to point B).
My array is structured as latitude1, longitude1, latitude2, longitude2 and so on.
I have to code to actually work out the distance (shown below), but don't know how to do the loop needed to get the coordinates.
function distance(lat1, lon1, lat2, lon2) {
var R = 6371;
var a =
0.5 - Math.cos((lat2 - lat1) * Math.PI / 180)/2 +
Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
(1 - Math.cos((lon2 - lon1) * Math.PI / 180))/2;
return R * 2 * Math.asin(Math.sqrt(a));
}
How should the loop be done?
Thanks.

There are several ways, I would do it like this:
int sum(int[] array) {
int sum = 0;
for(int i = 0; i < array.size(); i++) {
sum += distance(array[i], array[i+1], array[i+2], array[i+3]);
i +=4;
}
return sum;
}

Related

Leaflet.js Circle to Polygon Conversion

I'm using Leaflet.js to save coverage maps and am giving the user the option of using polygons or circles.
To keep all objects in the same format, I'm converting the circle to a polygon before saving.
However, when I then reload the circle it is oval-shaped.
I know that this is due to the earth's curve but I'm unsure how to correct my method to take this into account? (I've looked but can't find anything that gives the solution I'm after).
The main issue is the javascript method I'm using below as that doesn't take into account the earth's curve.
// GenerateCirlcePolygon - Creates Circle from 360 line Segments
function GenerateCirlcePolygon(origin, radius) {
var earthRadius = 6371;
//latitude in radians
var lat = (origin.Latitude * Math.PI) / 180;
//longitude in radians
var lon = (origin.Longitude * Math.PI) / 180;
//angular distance covered on earth's surface
var d = parseFloat(radius) / earthRadius;
polyPoints = new Array();
for (i = 0; i <= 360; i++) {
var point = new VELatLong(0, 0)
var bearing = i * Math.PI / 180; //rad
point.Latitude = Math.asin(Math.sin(lat) * Math.cos(d) + Math.cos(lat) * Math.sin(d) * Math.cos(bearing));
point.Longitude = ((lon + Math.atan2(Math.sin(bearing) * Math.sin(d) * Math.cos(lat), Math.cos(d) - Math.sin(lat) * Math.sin(point.Latitude))) * 180) / Math.PI;
point.Latitude = (point.Latitude * 180) / Math.PI;
polyPoints.push(point);
}
Any advice at all would be great.
You can use the built in function from leaflet-geoman: L.PM.Utils.circleToPolygon(circle, sides)
L.PM.Utils.circleToPolygon(circle, 60).addTo(map)

how to use self defined function mongodb

I'm trying to use a self-defined function in mongodb to calculate some values, but have no clue on how to use it.
There are avgStartLongitude, avgStartLatitudes, avgEndLongitude and avgEndLatitude. Grouped by an activity. It shows the location of an activity which started and ended somewhere. I need to calculate the distance between them by using a function.
This is the collection from where the calculation needs to be done:http://prntscr.com/d3cn4m
Following code shows (in a way that does not work) how I try to use the function to calculate:
function calcDistance(lat1, lon1, lat2, lon2) {
var R = 6371; // Radius of the earth in km
var dLat = (lat2 - lat1) * Math.PI / 180; // deg2rad below
var dLon = (lon2 - lon1) * Math.PI / 180;
var a =
0.5 - Math.cos(dLat)/2 +
Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
(1 - Math.cos(dLon))/2;
return (R * 2 * Math.asin(Math.sqrt(a)))<100;
};
db.opg3.aggregate(
{
$project:
{
activityType:"$_id",
avgDistance:"$calcDistance($avgStartLat,$avgStartLon,$avgEndLat,$avgEndLon)"
}
}
)
The code does not return the avgDistance. In fact it seems to do nothing and I have no clue how to make it work..
Any ideas on how I should use this function?
Thanks!

Using aviation formula (Lat/lon given radial and distance); longitude does not return

This is the formula for calculating a lat/lon given radial and distance, found here: http://williams.best.vwh.net/avform.htm#LL
lat=asin(sin(lat1)*cos(d)+cos(lat1)*sin(d)*cos(tc))
IF (cos(lat)=0)
lon=lon1 // endpoint a pole
ELSE
lon=mod(lon1-asin(sin(tc)*sin(d)/cos(lat))+pi,2*pi)-pi
ENDIF
Here is the code I am currently using (all inputs and outputs calculated in radians):
nx = Math.asin(Math.sin(lat0) * Math.cos(d) + Math.cos(lat0) * Math.sin(d) * Math.cos(c));
ny = mod(lon0 - Math.asin(sin(c) * Math.sin(d) / Math.cos(lat1)) + Math.PI, 2 * Math.PI) - Math.PI;
return [nx, ny];
}
nx prints with no problems, but ny does not print.
If mod is modulus operator
Change:
ny = mod(lon0 - Math.asin(sin(c) * Math.sin(d) / Math.cos(lat1)) + Math.PI, 2 * Math.PI) - Math.PI;
to
// ....................vvvvv note, I spotted this omission as well
ny =((lon0 - Math.asin(Math.sin(c) * Math.sin(d) / Math.cos(lat1)) + Math.PI) % (2 * Math.PI)) - Math.PI;
or create a function
function mod(a, b) {
return a % b;
}
but, you will need to fix up that lack of Math. above

Phonegap watchPosition measuring distance and trigger event

I'm developing a simple jQuery / Phonegap app and I'm using watchPosition to constantly calculate between the distance current location and some remote place. This works just fine, but this is my issue. I would like to trigger an event (function) when the distance is less than a mile from that location. The way I have it right now it would continue triggering the event as long as the distance is less than a mile. What can I do to "tell" watchPosition to trigger the event only once and continue watching my position?
I included what I consider the relevant part of the code for your reference.
Success portion of watchPosition:
var remoteLat = xx.xxxx;
var remoteLng = xx.xxxx;
function onSuccess(position) {
var myLat = position.coords.latitude;
var myLng = position.coords.longitude;
var distancia = gps_distance(myLat, myLng, remoteLat, remoteLng);
if (distancia < 1) {
alert('Almost There');
playSound();
}
}
Distance calculating function:
function gps_distance(lat1, lon1, lat2, lon2) {
var R = 3959; // use 3959 for miles or 6371 for km
var dLat = (lat2 - lat1) * (Math.PI / 180);
var dLon = (lon2 - lon1) * (Math.PI / 180);
var lat1 = lat1 * (Math.PI / 180);
var lat2 = lat2 * (Math.PI / 180);
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c;
return d;
}
If more of the code is necessary, please let me know. I also would like to give credit to this question previously asked about calculating distances Link . Thanks!
Just use a flag to track the alert.
var alerFlag = true;
if (distancia < 1 && alerFlag == true) {
alert('Almost There');
playSound();
alerFlag = false;
}else{
alerFlag = true; // This is for whenever you go greater than 1 mi
}

Javascript Distance Calculation Returns NaN

I have a function to calculate the distance between two sets of latitude and longitude coordinates I get from the Google Maps API
Here is the calculate distance function:
function getDistance(lat1, lon1, lat2, lon2) {
var lat1p = parseFloat(lat1);
var lon1p = parseFloat(lon1);
var lat2p = parseFloat(lat2);
var lon2p = parseFloat(lon2);
var R = 6371; // km (change this constant to get miles)
var dLat = (lat2p-lat1p) * Math.PI / 180;
var dLon = (lon2p-lon1p) * Math.PI / 180;
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1 * Math.PI / 180 ) * Math.cos(lat2 * Math.PI / 180 ) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
if (d>1) return Math.round(d)+"km";
else if (d<=1) return Math.round(d*1000)+"m";
return d;
}
I believe all the coordinates are considered Float types I'm not 100% sure but when I have it print or alert what the distance is it returns NaN.... Is there a way to do this. I'm trying to stray away from the Google Maps API to calculate distance because that would make everything harder because I would essentially have to start over.
I'm sorry I fixed it I was double declaring variables as well as I had a . instead of a , when I called the distance function. Thank you guys for your suggestions they led me to figure it out.

Categories

Resources