5

Hello everybody o/ I know that this is more a math question than gmap, but I suppose that someone already pass through this =)

In my map, I have circle (actually I have several of them, but this not change the question), like this: http://code.google.com/intl/pt-BR/apis/maps/articles/mvcfun/step6.html

How do I know if a marker (with latitude X and longitude Y) is inside this circle?

Sorry for the bad english, I'm brazillian =p

Lucas Pelegrino
  • 271
  • 1
  • 3
  • 5

5 Answers5

8

In Google Maps JavaScript API v3 you can use geometry library. To enable it you have to slightly change the script URL:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&libraries=geometry"></script>

The library contains utility functions for the computation of geometric data on sphere. You can utilize it to compute the distance of two points given by their latLngs this way:

var distanceInMetres = google.maps.geometry.spherical.computeDistanceBetween(latLngCircleCenter, latLngPoint);

Now you can easily check if the point is inside the circle (suppose R is in metres):

if(distanceInMetres < R)
   alert("in the circle");
Tomik
  • 23,857
  • 8
  • 121
  • 100
2

If (lat1, lon1) and (lat2, lon2) are your two points and R is the radius of the circle around your first point, then the distance between the points is given by the haversine formula (or the Great-circle distance). But I believe that for your problem, the angles are small enough to use this approximation:

Approximate Haversine formula

and then check whether d^2 is less than the radius R^2.

But if your latitude and longitude differences are larger than a few degrees, you'll want to use the full haversine formula.

marshall.ward
  • 6,758
  • 8
  • 35
  • 50
1

I Recommend you read http://www.movable-type.co.uk/scripts/latlong.html. It provides a number of algorithms for computations of this kind. It includes JavaScript code for the computations.

andand
  • 17,134
  • 11
  • 53
  • 79
0

Basically if you have the coords of circle center (cX,cY) and radius R, and some marker at X,Y you can do the following calculations:

var distanceQuad = (X-cX)*(X-cX)+(Y-cY)*(Y-cY)
if (distanceQuad<=(R*R))
{
 alert("Marker inside circle!");
}

This is from trigonometry. You calculate distance as sqrt(sqr(deltaX)+sqr(deltaY)) and compare it with circle Radius. Given code is a bit optimized to get rid of calculating square root.

  • I tried `var cX = 37.79457586821203, cY = -122.37932682031499, R = 50, X = 37.791591528681565, Y = -121.81147098535405; var distanceQuad = (X-cX)*(X-cX)+(Y-cY)*(Y-cY) if (distanceQuad<=(R*R)) { alert("Marker inside circle!");` But aways "Marker inside circle" – Lucas Pelegrino Feb 17 '11 at 14:56
  • This will work if you're concerned about linear distances. The case the OP is concerned about deals with great circle arc lengths which require haversine formulations. – andand Feb 22 '11 at 19:11
0

It's much easier than you'd expect. Read this answer including a working jsFiddle example.

Community
  • 1
  • 1
kaiser
  • 21,817
  • 17
  • 90
  • 110