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.
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)
}
You could just do the distance comparison manually, fairly trivially.
(x1 - x2)^2 + (y1 - y2)^2 <= D^2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With