Reputation: 137
I have this formula:
var bounds = map.getBounds();
var center = bounds.getCenter();
var ne = bounds.getNorthEast();
// r = radius of the earth in statute miles
var r = 3963.0;
// Convert lat or lng from decimal degrees into radians (divide by 57.2958)
var lat1 = center.lat() / 57.2958;
var lon1 = center.lng() / 57.2958;
var lat2 = ne.lat() / 57.2958;
var lon2 = ne.lng() / 57.2958;
// distance = circle radius from center to Northeast corner of bounds
var dis = r * Math.acos(Math.sin(lat1) * Math.sin(lat2) +
Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1));
It calculates radius by NE and center. I need: Formula to calculate NE coordinate by given radius and center on map written with js.
Upvotes: 0
Views: 890
Reputation: 4043
Ok, first I guess it will help to understand where the used formula comes from. And even before that, note that I will use standard mathematical coordinates. This differs from geographical long/lat but should be easy to transform
Hence a point on a sphere is (x,y,z)= r*(cos p sin t, sin p sin t, cos t)
. So p
is the angle from x
to y
and t
is the angle of the z
-axis.
If you have two points (p,t) and (q, u) we can rotate the first point to p=0, i.e. over the x
-axis. Than the points have coordinates (0,t)
and (q-p,u)
. Now we rotate the points around y
such that the first point becomes the north pole.
[ cos t, 0, -sin t] [x] [ cos t, 0, -sin t] [ cos(q-p) sin(u)]
[ 0 1, 0 ] . [y] = [ 0 1, 0 ] . [ sin(q-p) sin(u)]
[ sin t, 0, cos t] [z] [ sin t, 0, cos t] [ cos(u) ]
the new z
than is
z_new = sin(t) cos(q-p) sin(u) + cos(t)cos(u)
Naturally the arc length from here to the north pole is just
alpha = arcsin( sin(t) cos(q-p) sin(u) + cos(t)cos(u) )
and for the true distance we have to multiply with the radius r
of the sphere.
Now the other way. we have a point (p,t)
and want the (q,u)
given that its direction is an angle beta
off north and in distance d
. In a fist step we set point (p,t)
as north pole. That makes the second point (Pi + beta, d/r)
(Note angles are mathematical positive if ccw). This system has to be rotated such that the north pole goes to the given (p,t)
. This is done by
[ cos t, sin t, 0] [ cos p, 0, sin p] [x]
[ -sin p, cos t, 0] . [ 0 1, 0 ] . [y]
[ 0 , 0 , 1] [ -sin p, 0, cos p] [z]
setting (Pi + beta, d/r) = (gamma, theta)
we get
z_new = -sin(p)cos(gamma)sin(theta)+cos(p)cos(theta)
consequently:
u = arccos( z_new )
Finally:
x_new = cos(t) ( cos(p)cos(gamma)sin(theta) + sin(p)cos(theta) ) + sin(theta)sin(gamma)sin(theta)
As x_new = cos(q)sin(u)
and we know u
q = arccos( xnew / sin(u) ) = arccos( xnew / sqrt( 1 - z_new ) )
I hope I got it all right and remember this is in typical mathematical polar coordinates and it has to be translated to the sin/cos usage and angle definition in geography.
Upvotes: 1