19
votes

Below is the desired result, which I'm looking for

enter image description here

What I would like to know is:

I have created the circle using center point lat lang and radius around it. Now I want to know, how to check (calculate) if a latitude and longitude is either inside or outside the area I would appreciate if you can give me code example in Javascript. I'm using Google Maps API V3.

I found this function but not working as expected for me:

function arePointsNear(checkPoint, centerPoint) {
    var sw = new google.maps.LatLng(centerPoint.lat() - 0.005, centerPoint.lng() - 0.005);
    var ne = new google.maps.LatLng(centerPoint.lat() + 0.005, centerPoint.lng() + 0.005);
    var bounds = new google.maps.LatLngBounds(sw, ne);
    if (bounds.contains (checkPoint)){
        return true;
    }
    return false;
}

Any help will be great.. thanks in advance!!

3
A circle has a constant radius. If your circle has a 10km radius, check whether the point is within 10km to the circle center point. If it is, it means the point is within the circle.MrUpsidown

3 Answers

46
votes

For such short distances, and when the accuracy doesn't have to be exact to the centimeter, you can treat the surface of the earth as flat. Calculate a conversion from degrees to kilometers at the latitude of the center point, then the Pythagorean theorem can be used to get the distance:

function arePointsNear(checkPoint, centerPoint, km) {
  var ky = 40000 / 360;
  var kx = Math.cos(Math.PI * centerPoint.lat / 180.0) * ky;
  var dx = Math.abs(centerPoint.lng - checkPoint.lng) * kx;
  var dy = Math.abs(centerPoint.lat - checkPoint.lat) * ky;
  return Math.sqrt(dx * dx + dy * dy) <= km;
}

Demo: http://jsfiddle.net/Guffa/57gQa/

Note: The code doesn't take into consideration if you are passing the 0/360 longitude. If that is the case, you would have to normalize the longitudes first.

1
votes

All you need is a little spherical trig

First you need the central angle theta of the arc subtended by your distance (L = 10 km).

L = theta*r

where r is the radius of the earth (6378.135 km)

Now, if the central angle between the point of interest and your center point is < theta, it is inside your circle. Call this angle theta_p.

Here's a diagram illustrating a spherical triangle: spherical triangle image http://en.wikipedia.org/wiki/File:Spherical_trigonometry_basic_triangle.svg

edit - sorry, apparently I don't know how to link to an image?? Here's the URL: http://en.wikipedia.org/wiki/File:Spherical_trigonometry_basic_triangle.svg

In this case, two of the sides of the spherical triangle (call them a, b) are the difference in longitude and difference in latitude of the points respectively. The included angle C is 90 degrees (angle between lines of longitude and lines of latitude.

The spherical trig law of cosines is:

cos(c) = cos(a)*cos(b) + sin(a)*sin(b)*cos(C)

c is the central angle between your points, which we earlier called theta_p

edit - this solution isn't limited to small distance WRT the radius of the earth, as the other suggestions are.

0
votes

Use the Pythagorean theorem to validate. The distance from your center to the point you want to validate can be calculated as the hypotenuse of a triangle.