Search code examples
javascriptgoogle-maps-api-3point-in-polygon

Work out LatLng distance from LatLng (or point in circle) - Google Maps v3


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?


Solution

  • 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