The code below theoretically serves to compare all the Google Maps API bookmarks that I've integrated into the website, where custom markers have a radius of 750 meters, and I'm trying to calculate the radius and know if they overlap, and for that I walked searching the internet and incrementing the basic code of the APIv3 plus the code of javascript functions for the calculation to check if the markers' rays meet.
Up to the map display part, bookmarks in addresses registered in the database table (110 total) plus address reservation addresses (5 total) have been displayed, but at the end of the setMarkers ( ) ; I have a for in calling another function, the hasIntersections (); function, and then the error occurs.
"Uncaught TypeError: circle0.getCenter is not a function"
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=XXXXXXXXXXXXXXXXXXXXXXXX"></script><script>vargeocoder;varmap;varmarker;varimage='https://www.keepidea.com.br/painel/assets/images/mark-teste.png';varlocations2=[['RESERVA:',-23.9446791,-46.3264323,'Av.SenadorFeijó,421-VilaNova,Santos-SP,11013-162,Brasil'],['RESERVA:',,,'R.Assunção,414-Sion,BeloHorizonte-MG,30320-040,Brasil'],['RESERVA:',,,'R.PadreFeijó,373-VilaTiberio,RibeirãoPreto-SP,14050-360,Brasil'],['RESERVA:',-23.0654964,-47.2263359,'R.VitorianoSalut,72-ChácaraBelvedere,Indaiatuba-SP,Brasil'],['RESERVA:',-20.8873894,-47.5997438,'RuaAnaLuiza,197-BairroCastelo,Batatais-SP,14300-000,Brasil']];varlocations=[['VilaOsasco',-23.544559,-46.781034,'AvenidaSantoAntônio,2761,VilaOsasco,-SP,06083-215,Brasil'],['Rebouças',-22.8904057,-43.5603237,'AvenidaRebouças,3657,Centro,-SP,13170-023,Brasil'],['NutritionBauru',-22.3370765,-49.0891827,'AvenidaCasteloBranco,7-15,VilaIndependência,-SP,17052-000,Brasil'],['Pitangueiras',-23.2022764,-46.8836439,'RuaPitangueiras,790,JardimPitangueiras,-SP,13206-716,Brasil'],['ValedoSol',-20.4229151,-49.9592271,'Avenidapansani,3092,Valedosol,-SP,15500-302,Brasil'],['CidadeNovaMontesClaros-Mg',-16.739527,-43.8653989,'AvenidaDonaGregória,101,CidadeNova,-MG,39400-464,Brasil'],['CentroSalto',-23.199741,-47.3008818,'AvenidaDomPedroII,1421,Centro,-SP,13320-241,Brasil'],['ASSIS',-22.6575047,-50.397561,'RuaAntôniodaSilvaCunhaBueno,425,JardimPaulista,-SP,19815-080,Brasil'],['Paulinia',-22.743178,-47.1744984,'AvenidaJoãoAranha,946,AltodePinheiros,-SP,13145-256,Brasil'],['SBCCentro',-23.7132318,-46.5532384,'RuaJoaquimNabuco,56,Centro,-SP,09720-375,Brasil'],['Chácara',-23.6290144,-46.7025941,'Ruadapaz,1498,ChácaraSantoAntônio,-SP,04713-000,Brasil'],['NovaEuropa',-22.9424831,-47.0573369,'AvenidaBadenPowell,1831,NovaEuropa,-SP,13040-093,Brasil'],['MoradadoSol',-23.1269188,-47.244239,'RuaJoãoMartini,475,JardimMoradadoSol,-SP,13348-350,Brasil'],['CampograndeRJ',-22.890374,-43.560187,'Ruadopetróleo,14,CampoGande,-RJ,23087-125,Brasil'],['Tremembé',-23.4587589,-46.6149244,'AvenidaNovaCantareira,4794,Tucuruvi,-SP,02340-002,Brasil'],['ViladasBelezas',-23.6491279,-46.7543918,'EstradadeItapecerica,2219,ViladasBelezas,-SP,05835-005,Brasil'],['CCentro',-23.0854053,-47.2105986,'RuaHumaitá,1099,Centro,-SP,13339-140,Brasil'],['ITAPECERICA',-23.698334,-46.8527042,'RuaAriBatalha,393,ParqueParaiso,-SP,06850-335,Brasil'],['lapa',-23.5250071,-46.6872423,'RuaClélia,911,Lapa,-SP,05042-000,Brasil'],['InteraçãoFlamboyant',-17.7718674,-48.6186493,'RuaB-02,S/N,EstânciaItanhanga,-GO,75690-000,Brasil'],['Tatuapé',-23.552282,-46.5566401,'RuaFranciscoMarengo,1717,Tatuapé,-SP,03313-001,Brasil'],['Flamboyant',-22.8762222,-47.0358206,'RuaComendadorDoutorAntônioPompeoCamargo,104,JardimBoaEsperança,-SP,13091-502,Brasil'],['N.SrdeFatima',-22.8975439,-47.1685964,'ArmelindaEspúriodaSilva,565,JardimNossaSenhoradeFátima,-SP,13084-450,Brasil'],['Agapeama',-23.2053398,-46.8651296,'RuadaVarzeaPaulista,487,VilaAgricola,-SP,13202-700,Brasil'],['Buriti',-23.2230818,-46.8760877,'RuaDomPedroI,462,JardimBuriti,-SP,13225-790,Brasil'],['BOAVISTA',-14.8793362,-40.832031,'AvenidaGilenildaAlves,1345,BoaVista,-BA,45027-560,Brasil'],['FABRICADERESULTADOS',-23.6008617,-46.8988726,'RuaPinhal,83,JardimSabia,-SP,06716-575,Brasil'],['ValentinaCrossLife',-16.7287284,-43.8835938,'RuaEspíritoSantoEsplanada,395,Esplanada,-MG,39401-452,Brasil'],['PEDROPINHO',-23.5402247,-46.8010548,'AvenidaGeneralPedroPinho,1052,Pestana,-SP,06122-160,Brasil']];functioninitMap(){varlatlng=newgoogle.maps.LatLng(-84.568808,-100.418683);varoptions={zoom:4,center:latlng,mapTypeId:google.maps.MapTypeId.ROADMAP};map=newgoogle.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);
}
Number.prototype.toRadians = function() {
return this * (Math.PI / 180.0);
};
function distance(lat0, long0, lat1, long1)
{
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;
}
circles = [];
circles2 = [];
circle = null;
circle2 = null;
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
});
var cityCircle = new google.maps.Circle({
map: map,
zoom: 4,
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 in circles )
{
for ( var circle2 in circles2 )
{
console.log(hasIntersections(circle1, circle2));
}
}
}
</script>
I made a test in the console, with console.log (circles), and the result was a total of 5 results like:
_. qg {gm_accessors_: {...}, map: gg, gm_bindings_: {...}, zoom: 4, center: _.L, ...} A: : {...}, b: "planetRadius", f: false, getPlanetRadius: ƒ, ...} center: Ƒ c () editable_changed: ƒ and () f: gg {gm_bindings_: {...}, __gm: Cf, gm_accessors_: {...}, zoom: 4, center: _.L, ...} fillColor: "# 49DA45" fillOpacity: 0.5 gm_accessors_: {map: null, zoom: null, center: null, radius: null, strokeColor: null, ...} gm_bindings_: {map: {...}, zoom: {...}, center: {...}, radius: {...}, strokeColor: {...}, ...} hh: hW {Ck: 0, __e3_: {...}, b: _.sg, gm_bindings_: {...}, gm_accessors_: {...}, ...} ja: (9) [Mc, Mc, Mc, Mc, Mc, Mc, Mc, Mc, Mc] map: gg {gm_bindings_: {...}, __gm: Cf, gm_accessors_: {...}, zoom: 4, center: _.L, ...} radius: 750 strokeColor : "# 229A1F" vh: (3) [Mc, Mc, Mc] visible: true zoom: 4 __e3_: {toolbar: {...}, mouseover: {...}, click: {...}, mouseout: {...}} __proto: Object
And for console.log (circles2) the result in the total of 110, for circles and circles2 the values beat, because for var location [] has 109 and var location [] has 5, total of 115, which has registered in the database.