Pergunta

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?

Foi útil?

Solução

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

Outras dicas

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

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top