How to determine if a point is in a circle?

How can I check if a LatLng is within a circle? (Google Maps JavaScript v3)

The getBounds () method returns a bounding rectangle for a circle, which is a rectangle, so if the point extends beyond the circle but inside the bounding box, you will receive an incorrect answer.

+6
source share
6 answers

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

function pointInCircle(point, radius, center) { return (google.maps.geometry.spherical.computeDistanceBetween(point, center) <= radius) } 
+11
source

You could just do a distance comparison manually, quite trivially.

 (x1 - x2)^2 + (y1 - y2)^2 <= D^2 
+2
source

You can use the Circle object to display it;

 new google.maps.Circle({ map : map, center : new google.maps.LatLng(lat,lng), strokeColor:'#00FFCC', strokeWeight:2, fillOpacity:0, radius:radiusm }); 

And we apply the Pythagorean theorem to the coordinates: but in this case make it a “real” circle, since the ratio between 1 ° latitude and longitude varies at different latitudes, you should at least configure them like this:

 var kmRadius = 100; //(radius of 100 km) var lat_gap = kmRadius/111.1; var lng_gap = lat_gap / Math.cos(lat / (Math.PI/180)); 
+1
source

Why don't you calculate this using the Pythagorean theorem ? You know a² + b² = c². If c is less than r (radius), you know that it is inside.

 var isInside=Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2) >= r*r; 
0
source

Something like this should do the trick (code not tested):

 public boolean pointInCircle(Circle c, LatLng coord) { Rectangle r = c.getBounds(); double rectX = r.getX(); double rectY = r.getY(); double rectWidth = r.getWidth(); double rectHeight = r.getHeight(); double circleCenterX = rectX + rectWidth/2; double circleCenterY = rectY + rectHeight/2; double lat = coord.getLatitude(); double lon = coord.getLongitude(); // Point in circle if (x−h)^2 + (y−k)^2 <= r^2 double rSquared = Math.pow(rectWidth/2, 2); double point = Math.pow(lat - circleCenterX, 2) + Math.pow(lon - circleCenterY, 2); return (point <= rSquared) ? true : false; } 
0
source

Try this (Javascript):

 const toRadians = (val) => { return val * Math.PI / 180; } const toDegrees = (val) => { return val * 180 / Math.PI; } // Calculate a point winthin a circle // circle ={center:LatLong, radius: number} // in metres const pointInsideCircle = (point, circle) => { let center = circle.center; let distance = distanceBetween(point, center); return distance < circle.radius; // Use '<=' if you want to get all points in the border }; const distanceBetween = (point1, point2) => { var R = 6371e3; // metres var φ1 = toRadians(point1.latitude); var φ2 = toRadians(point2.latitude); var Δφ = toRadians(point2.latitude - point1.latitude); var Δλ = toRadians(point2.longitude - point1.longitude); var a = Math.sin(Δφ / 2) * Math.sin(Δφ / 2) + Math.cos(φ1) * Math.cos(φ2) * Math.sin(Δλ / 2) * Math.sin(Δλ / 2); var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); return R * c; } 

Links: http://www.movable-type.co.uk/scripts/latlong.html

0
source

All Articles