I have a Google Map that has multiple circles around multiple points which I've drawn the following way:
for (i = 0; i < markers.length; i++) {
var circle = new google.maps.Circle({
map: map,
radius: parseInt(radius_entry[markers[i][0]]),
fillColor: '#2c3e50',
strokeColor: '#2c3e50',
strokeOpacity: 0
});
var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
pinIcon = new google.maps.MarkerImage(
"assets/images/station_maker.png",
null, /* size is determined at runtime */
null, /* origin is 0,0 */
null, /* anchor is bottom center of the scaled image */
new google.maps.Size(21, 34)
);
marker = new google.maps.Marker({
position: position,
map: map,
title: markers[i][0],
});
marker.setIcon(pinIcon)
circle.bindTo('center', marker, 'position');
google.maps.event.addListener(circle, 'click', function(ev) { //Placing marker only on clicks inside a circle
placeMarker(ev.latLng);
});
bounds.extend(position);
map.fitBounds(bounds);
}
I have also provided a method for users to search for a location using the places library on the same map which is a very straightforward implementation.
var input = document.getElementById('search-places');
var searchBox = new google.maps.places.SearchBox(input);
google.maps.event.addListener(searchBox, 'places_changed', function() {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
var bounds = new google.maps.LatLngBounds();
for (var i = 0, place; place = places[i]; i++) {
bounds.extend(place.geometry.location);
}
map.fitBounds(bounds);
});
However, I want to add a mechanism that checks if the searched place falls within any of the circles drawn earlier. I found this method:
polygon.containsLocation()
https://developers.google.com/maps/documentation/javascript/examples/poly-containsLocation
But this didn't work for circles.
I also found this SO question How to detect if a point is in a Circle? but it deals with only one circle drawn on a map. How do I achieve this scenario?