How do I know if a Lat,Lng point is contained within a circle?

Stuart Beard picture Stuart Beard · Dec 16, 2010 · Viewed 30.8k times · Source

Ok pretty self explanatory. I'm using google maps and I'm trying to find out if a lat,long point is within a circle of radius say x (x is chosen by the user).

Bounding box will not work for this. I have already tried using the following code:

distlatLng = new google.maps.LatLng(dist.latlng[0],dist.latlng[1]);
var latLngBounds = circle.getBounds();
if(latLngBounds.contains(distlatLng)){
      dropPins(distlatLng,dist.f_addr);
}

This still results in markers being places outside the circle.

I'm guess this is some simple maths requiring the calculation of the curvature or an area but I'm not sure where to begin. Any suggestions?

Answer

kaiser picture kaiser · Dec 16, 2011

Working solution with dragable center marker

Have you ever tried contains? Take a look at the LatLngBounds Constructor.

I wrote an article about it, that contains a link to a working JSFiddle.net example.

Screenshot of jsFiddle


Updated version.