Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if a point is in a Circle?

How can I test if a LatLng point is within the bounds of a circle? (Google Maps JavaScript v3)

The getBounds() method returns the bounding box for the circle, which is a rectangle, so if a point falls outside the circle but within the bounding box, you'll get the wrong answer.

like image 488
Michael Schade Avatar asked Jan 30 '13 07:01

Michael Schade


2 Answers

Use the spherical geometry library (be sure to include it with the API)

function pointInCircle(point, radius, center)
{
    return (google.maps.geometry.spherical.computeDistanceBetween(point, center) <= radius)
}
like image 77
geocodezip Avatar answered Sep 22 '22 04:09

geocodezip


You could just do the distance comparison manually, fairly trivially.

(x1 - x2)^2 + (y1 - y2)^2 <= D^2 
like image 35
sheu Avatar answered Sep 22 '22 04:09

sheu