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.
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.LatLngclass: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
pointAnear the north pole, with a radius of 1,000km:Screenshot for
pointA.destinationPoint(90, radius):