0
votes

I'm attempting to find the longitude (east-west) angle of a new point given that latitude does not also change.

Given: Starting Latitude, Starting Longitude, distance of travel in miles.

Desired result: Maximum and Minimum Longitude and Latitude degrees that are "distance of travel" from starting point.

Code below:

Attempt 1: Use "close enough" constants:

function(longitude, latitude, travelRadius, next){
var milesPerDegreeOfLongitude = 69.172;
var milesPerDegreeOfLatitude = 69.2;
var location = { longitude : longitude, latitude : latitude };

var longitudeDelta = (travelRadius / milesPerDegreeOfLongitude); 
location.maxLongitude = longitude + longitudeDelta;
location.minLongitude = longitude - longitudeDelta;

var latitudeDelta = (travelRadius / milesPerDegreeOfLatitude);
location.maxLatitude = latitude + latitudeDelta;
location.minLatiude = latitude - latitudeDelta;

next(location);
}

^ This yields very close latitude, for a distance of 5 miles. But longitude for the same is about 3.93 miles as measured by google maps, too high of a variance.

Attempt 2: One radian is the angle for a line 3,958.761 miles long (the Earth's radius) therefore the angle for a line X miles long is X/3,958.761 radians.

 function(longitude, latitude, travelRadius, next){
 var radiusOfEarth = 3959.761;
 //... omitting latitude ...
 var distanceInRadians = (travelRadius / radiusOfEarth);
 var degrees = (180 / Math.PI) * distanceInRadians;
 location.maxLongitude = longitude + degrees;
 location.minLongitude = longitude - degrees;
 next(location)
 }

^ And again I end up with about 3.93 miles on longitude distance. What could I do to get this right or at least within .5 miles of error?

Edit: attempt 3 using Mbo's suggestion:

var latitude = 37.7118042;
var longitude = -122.4458397;
var travelradius = 5;

function(latitude, longitude, travelRadius){

var milesPerDegreeOfLatitude = 69.172;
var milesPerDegreeOfLongitude = 69.172 * Math.cos(latitude)
var location = { longitude : longitude, latitude : latitude };

var latitudeDelta = (travelRadius / milesPerDegreeOfLatitude);
location.maxLatitude = latitude + latitudeDelta;
location.minLatiude = latitude - latitudeDelta;

var longitudeDelta = (travelRadius / milesPerDegreeOfLongitude); 
location.maxLongitude = longitude + longitudeDelta;
location.minLongitude = longitude - longitudeDelta;

return location;   
}

Outputs:

{ 
longitude: -122.4458397,                                                                                                                          
latitude: 37.7118042,                                                                                                                             
maxLongitude: -122.37355611704736,                                                                                                                
minLongitude: -122.51812328295263,                                                                                                                
maxLatitude: 37.784058535260115,                                                                                                                  
minLatiude: 37.63954986473989 
} 

Where Max longitude is ~3.85 miles from starting position. Still off, expecting something like: -122.3515 which is ~5 miles

1
Please specify the problem better - what is given and what you want to find?MBo
Added what is given and what is desired result.Eric H

1 Answers

1
votes

milesPerDegreeOfLongitude is not constant value!

It depends on latitude. 69.172 is value for equator (zero latitude).
For other latitudes:

trueMilesPerDegreeOfLongitude = 69.172 * Cos(latitude)

Note that you code uses degrees, but Math.cos works with radians:

var latitude = 37.7118042;
var milesPerDegreeOfLongitude = 69.172 * Math.cos(latitude)

try something like

var milesPerDegreeOfLongitude = 69.172 * Math.cos(latitude * Math.Pi / 180)

My approx. calculation gives 0.09 for latitudeDelta and 122.44+-0.09 = 122.35, 122.53