To draw a circle on map I have a center GLatLng (A) and a radius (r) in meters.
Here's a diagram:
-----------
--/ \--
-/ \-
/ \
/ \
/ r \
| *-------------*
\ A / B
\ /
\ /
-\ /-
--\ /--
-----------
How to calculate the GLatLng at position B? Assuming that r is parallel to the equator.
Getting the radius when A and B is given is trivial using the GLatLng.distanceFrom() method - but doing it the other way around not so. Seems that I need to do some heavier math.
Javascript for many geodesic calculations (direct & inverse problems, area calculations, etc). is available at
http://geographiclib.sourceforge.net/scripts/geographiclib.js
Sample usage is shown in
http://geographiclib.sourceforge.net/scripts/geod-calc.html
An interface to google maps is provided at
http://geographiclib.sourceforge.net/scripts/geod-google.html
This includes plotting a geodesic (blue), geodesic circle (green) and the geodesic envelope (red).
To calculate a lat,long point at a given bearing and distance from another you can use google´s JavaScript implementation:
See https://developers.google.com/maps/documentation/javascript/reference#spherical For documentation
The answer to this question and more can be found here: http://www.edwilliams.org/avform.htm
We will need a method that returns the destination point when given a bearing and the distance travelled from a source point. Luckily, there is a very good JavaScript implementation by Chris Veness at Calculate distance, bearing and more between Latitude/Longitude points.
The following has been adapted to work with the
google.maps.LatLng
class:You would simply use it as follows:
Here is a complete example using Google Maps API v3:
Screenshot:
UPDATE:
In reply to Paul's comment below, this is what happens when the circle wraps around one of the poles.
Plotting
pointA
near the north pole, with a radius of 1,000km:Screenshot for
pointA.destinationPoint(90, radius)
:If you are after the distance between 2 lat/lng points across the earths surface then you can find the javascript here:
http://www.movable-type.co.uk/scripts/latlong-vincenty.html
This is the same formula used in android API at
android.location.Location::distanceTo
You can easily convert the code from javascript to java.
If you want to calculate the destination point given the start point, bearing and distance, then you need this method:
http://www.movable-type.co.uk/scripts/latlong-vincenty-direct.html
Here are the formulae in java: