I am trying to make a circle in google maps that shows the markers that are inside the radius zone of the circle, and when the map is moved the circle will follow. Like this example:
The problem I am having is the following:
Instead of getting the circle area, I am getting a rectangle area, the code I am using for this specific part is the following:
var myCity = new google.maps.Circle({
center: {lat: 38.7196268, lng: -9.1457501},
radius: 15000,
strokeColor: "rgb(0,0,0)",
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: "rgb(0,0,0)",
fillOpacity: 0.4
});
myCity.setMap(map);
var bounds = myCity.getBounds();
var it = 0;
for (var i = 0; i <=ct; i++) {
it=it+1;
if(bounds.contains(coordinate[it])){
marker[it].setVisible(true);
}
else{
marker[it].setVisible(false);
}
}
ct is a dynamic variable and for now has 207 values.
Here is the full script for the map:
<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 11,
center: {lat: 38.7196268, lng: -9.1457501},
disableDefaultUI: true,
zoomControl: true,
scrollwheel: false,
navigationControl: false,
mapTypeControl: false,
scaleControl: false
});
var ct=0;
var coordinate = [];
var marker = [];
var htmlString = "<?php while($row = $result->fetch_assoc()) { ?>";
ct++;
var rowGPSLon = "<?php echo $row['GPSLon']; ?>";
var rowGPSLat = "<?php echo $row['GPSLat']; ?>";
var pageID = "<?php echo $row['ID']; ?>";
rowGPSLon = parseFloat(rowGPSLon);
rowGPSLat = parseFloat(rowGPSLat);
coordinate[ct] = {lat: parseFloat(rowGPSLat), lng: parseFloat(rowGPSLon)};
marker[ct] = new google.maps.Marker({
position: coordinate[ct],
map: map,
url: "http://localhost?id=" + pageID,
});
var htmlString2 = "<?php } ?>";
var myCity = new google.maps.Circle({
center: {lat: 38.7196268, lng: -9.1457501},
radius: 15000,
strokeColor: "rgb(0,0,0)",
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: "rgb(0,0,0)",
fillOpacity: 0.4
});
myCity.setMap(map);
//Retrive the center location
google.maps.event.addListener(map, "center_changed", function() {
var center = map.getCenter().toUrlValue().split(",");
myCity.setCenter({lat: parseFloat(center[0]), lng: parseFloat(center[1])});
var bounds = myCity.getBounds();
var it = 0;
for (var i = 0; i <=ct; i++) {
it=it+1;
if(bounds.contains(coordinate[it])){
marker[it].setVisible(true);
}
else{
marker[it].setVisible(false);
}
}
});
var bounds = myCity.getBounds();
var it = 0;
for (var i = 0; i <=ct; i++) {
it=it+1;
if(bounds.contains(coordinate[it])==false){
marker[it].setVisible(false);
}
google.maps.event.addListener(marker[it], 'click', function() {
jQuery(".mapLoad").load(this.url);
jQuery(".mapLoadContainer").show(1000);
});
}
}
</script>
My question is, how can I get the area of the circle instead of a rectangle.