1

I need to find out if certain LatLngs are inside a Google Maps Circle (one of these: http://code.google.com/apis/maps/documentation/javascript/overlays.html#Circles). How would I get around doing this? My markup for making the circle is:

geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        circlemarker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });
        THEradius = parseFloat(THEradius);
         var populationOptions = {
          strokeColor: "#BDAEBB",
          strokeOpacity: 0.8,
          strokeWeight: 2,
          fillColor: "#BDAEBB",
          fillOpacity: 0.5,
          map: map,
          center: results[0].geometry.location,
          radius: THEradius
         };
        cityCircle = new google.maps.Circle(populationOptions);
        map.fitBounds(cityCircle.getBounds());
    }
});

Could I just use the radius?

rickyduck
  • 4,030
  • 14
  • 58
  • 93

3 Answers3

2
var distance = google.maps.geometry.spherical.computeDistanceBetween(
       results[0].geometry.location, otherLatLng);

if (distance <= THEradius) {...} else {...}

I hope that works for you. See http://code.google.com/apis/maps/documentation/javascript/reference.html#spherical

kargeor
  • 136
  • 2
  • by the way, you **can't** use Pythagorean theorem because the earth is not flat! – kargeor Sep 01 '11 at 13:39
  • Great first answer on the site, thanks a lot, definitely the best solution and the one I was looking for. Do you know if there is any request limits to this option? – rickyduck Sep 01 '11 at 13:47
  • 1
    The documentation does not mention any limits. I'm not sure if the function is evaluated locally or on the server. – kargeor Sep 01 '11 at 14:36
1

What you need to do is to convert either your lat-lon list into google coordinate space, or the circle into lat-lon coordinate space.

How you do the conversion depends on the language you are using, but there are websites that will do the conversion for you if it is a one off.

Once you've got the lat-lon locations in the same co-ordinate space as your circle, you can use simple pythagoras math to work out if the location is less than the radius of the circle (as you suggest).

HYP = (OPP^2 * ADJ^2)^0.5

Where:

OPP is the difference in x direction from the centre of the circle
ADJ is the difference in y direction from the centre of the circle.
HYP is the distance in a straight line from the centre of the circle
extorn
  • 611
  • 1
  • 5
  • 11
1

In terms of mathematics, to find the distance from one point to another in 2D, use Pythagoras:

X = X1 - X2
Y = Y1 - Y2

(The above effectively calculates a vector from one point to another)

Distance from 1 to 2 = sqrt(X^2 + Y^2)

Then you can compare that to your radius. If the distance is less than your radius, the point is within the circle.

You need to first acquire the point corresponding to the centre of the circle and the point you are trying to compare. These must be in the same co-ordinate space.

Spycho
  • 7,698
  • 3
  • 34
  • 55