0
The map correctly loads, the markers in the addresses coming from the bank, until then everything well, then happens a check if it has a marker superimposing the other, inside a if, I want to change the color of the circle, exactly the values of strokeColor and fillColor within the condition.
I searched the community and Google Maps documentation but found nothing, only how to customize at load time, and not how to change after the bookmarks already loaded on the map.
I noticed that the code is changing to red, but only the last address of the array, there is error in this for, there is another way to do this validation, because the function hasIntersections(). responds well by returning true or false, but as I said, only the last address that marks red, overlaid or not. The change should only be made if it returns true.
<div id="mapa" style="height: 600px; width: 100%"></div>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBkKQ9rh8Aimtsbn-Br6ppYwT8qbg6OCfw">
<script>
var geocoder;
var map;
var marker;
var image = 'https://www.site.com/assets/images/mark-01.png';
var locations = [[.....],[.....],[.....]];
var locations2 = [[.....],[.....],[.....]];
Number.prototype.toRadians = function() {
return this * (Math.PI / 180.0);
};
function distance(lat0, long0, lat1, long1){
// converter graus para radianos
var rlat0 = lat0.toRadians();
var rlong0 = long0.toRadians();
var rlat1 = lat1.toRadians();
var rlong1 = long1.toRadians();
var deltaLat = (rlat1-rlat0);
var deltaLong = (rlong1-rlong0);
var a = Math.pow(Math.sin(deltaLat / 2), 2) + Math.pow( Math.sin(deltaLong / 2), 2) * Math.cos(rlat0) * Math.cos(rlat1);
return (2 * Math.asin(Math.sqrt(a))) * 6378137;
}
function hasIntersections(circle0,circle1){
var center0 = circle0.getCenter();
var center1 = circle1.getCenter();
var maxDist = circle0.getRadius()+circle1.getRadius();
var actualDist = distance(center0.lat(),center0.lng(),center1.lat(),center1.lng());
return maxDist>=actualDist;
}
function initMap() {
var latlng = new google.maps.LatLng(-84.568808, -100.418683);
var options = {
zoom: 4,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("mapa"), options);
geocoder = new google.maps.Geocoder();
marker = new google.maps.Marker({
map: map,
zoom: 4
});
marker.setPosition(latlng);
setMarkers(map,locations);
setMarkers(map,locations2);
}
circles = [];
circles2 = [];
function setMarkers(map,locations) {
var marker = null;
var i = null;
for (i = 0; i < locations.length; i++) {
var loan = locations[i][0]
var lat = locations[i][1]
var long = locations[i][2]
var add = locations[i][3]
latlngset = new google.maps.LatLng(lat, long);
var marker = new google.maps.Marker({
map: map,
title: loan,
position: latlngset,
icon: image
});
var cityCircle = new google.maps.Circle({
map: map,
zoom: 4,
center: new google.maps.LatLng(lat, long),
radius: 750,
strokeColor: "#818c99",
fillColor: "#ffffff",
fillOpacity: 0.50
});
circles.push(cityCircle);
map.setCenter(marker.getPosition());
var content = "<h5>" + loan + '</h5>' + "<strong>Endereço:</strong> " + add;
var infowindow = new google.maps.InfoWindow()
google.maps.event.addListener(marker,'click', (function(marker,content,infowindow){
return function() {
infowindow.setContent(content);
infowindow.open(map,marker);
};
})(marker,content,infowindow));
}
var marker = null;
var i = null;
for (i = 0; i < locations2.length; i++) {
var loan = locations2[i][0]
var lat = locations2[i][1]
var long = locations2[i][2]
var add = locations2[i][3]
latlngset = new google.maps.LatLng(lat, long);
var marker = new google.maps.Marker({
map: map,
title: loan,
position: latlngset,
icon: image
});
cityCircle = new google.maps.Circle({
map: map,
zoom: 6,
center: new google.maps.LatLng(lat, long),
radius: 750,
strokeColor: "#229A1F",
fillColor: "#49DA45",
fillOpacity: 0.50
});
circles2.push(cityCircle);
map.setCenter(marker.getPosition());
var content = "<h5>" + loan + '</h5>' + "<strong>Endereço:</strong> " + add;
var infowindow = new google.maps.InfoWindow()
google.maps.event.addListener(marker,'click', (function(marker,content,infowindow){
return function() {
infowindow.setContent(content);
infowindow.open(map,marker);
};
})(marker,content,infowindow));]
}
for ( var circle1 of circles ) {
for ( var circle2 of circles2 ) {
if( hasIntersections(circle1, circle2) === true ) {
cityCircle.setOptions({
map: map,
zoom: 4,
center: new google.maps.LatLng(lat, long),
radius: 750,
fillColor: '#ba1e21',
strokeColor: '#ba1e21',
fillOpacity: 0.50
});
}
}
}
}
$(document).ready(function(){
$('body').attr('onload', 'initMap();');
});
</script>
Add your code
– Anderson Henrique
@Andersonhenrique Feito !
– ElvisP
Just to understand in case he carries white for example, you want with some action he change the color?
– Anderson Henrique
No, the map loads all, with the markers in the addresses coming from the bank, so far so good, but after checking if there is a marker superimposing the other, inside an if, is where I want to apply the change of the marker to another color (the circle), exactly the values of strokeColor and fillColor
– ElvisP
@Eliseub. take a look at my answer
– Leonardo Bonetti
@Andersonhenrique I raised the question for better accuracy.
– ElvisP
The @Leonardobonetti response will work
– Anderson Henrique