-2

I need to limit the radius of the great circle problem. The circle will extend until it hits another item.

I need it to limit the range of the circle to 5 miles

Here is my code

function find_closest_ticket(ticket, lat, lng) {

//  var lat = map.position.coords.latitude;
//  var lon = map.position.coords.longitude;

//  lat = 24.709254;
//  lng = -81.381927;
var R = 6371; // radius of earth in km
var distances = [];
var closest = -1;

for (i = 0; i < ticket.length; i++) {
  var mlat = ticket[i].soLAT;
  var mlng = ticket[i].soLNG;
  var dLat = rad(mlat - lat);
  var dLong = rad(mlng - lng);
  var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
  Math.cos(rad(lat)) * Math.cos(rad(lat)) * Math.sin(dLong / 2) * Math.sin(dLong / 2);
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  var d = R * c;
  distances[i] = d;
  if (closest == -1 || d < distances[closest]) {
  closest = i;
  }
  }
return closest;
}
pithhelmet
  • 2,222
  • 6
  • 35
  • 60
  • You forgot to ask a question. – JJJ Oct 22 '15 at 20:22
  • The question was posted on line two I need it to limit the range of the circle to 5 miles – pithhelmet Oct 22 '15 at 20:23
  • That is a statement, not a question. What's the problem you're having? – JJJ Oct 22 '15 at 20:24
  • I need to limit the range to file miles – pithhelmet Oct 22 '15 at 20:26
  • How do i limit the range to five miles or 8 km? – pithhelmet Oct 22 '15 at 20:26
  • Still not clear, sorry. Do you want a position (in lat,long) that is some distance (here: 5 miles) away from the given starting point (in lat,long)? – deamentiaemundi Oct 22 '15 at 20:29
  • OK - Currently, it appears the great circle will range out at 6371km. If i set the value of R to 8, will it limit the range of search to 8km? – pithhelmet Oct 22 '15 at 20:33
  • With r = 6371km it will calculate the segment of a circle of a radius of 6371km between the two given points (in lat/long); with r = 8km it will calculate the segment of a circle of a radius of 8km between the two given points (in lat/long), that is: you just made the earth smaller. That is most probably not what you want, so try to refine your question. Maybe with a little picture? – deamentiaemundi Oct 22 '15 at 20:39
  • right - thank you, i understand now. i will change my code to check the distance itself, and if it is greater than 8km. thank you – pithhelmet Oct 22 '15 at 20:51

1 Answers1

0

First, it is surprising you use a function that returns a radius in km, and then want to limit it to 5 miles. You should make up your mind: either write the function to return miles and add the limit in miles, or leave the function as-is and limit it by km (8 km is roughly 5 miles).

If you want to use miles, then change this line:

var R = 6371; // radius of earth in km

to:

var R = 3959; // radius of earth in miles

and replace:

return closest;

by:

return Math.min(5, closest);

Alternatively, if you want to stick to km, then only replace:

return closest;

by:

return Math.min(8, closest);
trincot
  • 317,000
  • 35
  • 244
  • 286