Created
September 30, 2018 03:47
-
-
Save avdata99/60f3314b333fffb7cb5f7e0e8cece971 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// requiere en variable map el mapa de google | |
function separar_superpuestos(round_decs=7) { | |
// revisar todo el mapa y detectar puntos superpuestos (o cercanos segun round de lat y lng) | |
let to_round = Math.pow(10, round_decs); | |
map.data.forEach(function(feature){ | |
let geom = feature.getGeometry(); | |
if (geom != null) { | |
let lat = geom.get().lat(); // ej -31.3382113 | |
let lng = geom.get().lng(); // ej -64.2942529 | |
let new_lat = Math.round(lat * to_round) / to_round; | |
let new_lng = Math.round(lng * to_round) / to_round; | |
// agrupar a los superpuestos | |
let idx = 'i' + new_lat + '_' + new_lng; | |
if (! points.hasOwnProperty(idx)){ | |
points[idx] = []; | |
} | |
// agrupando superpuestos | |
points[idx].push(feature); | |
feature.setProperty('hermanos', []); | |
} | |
}); | |
// avisarle a cada punto que tiene hermanos y separarlos | |
// ver a que distancia se los separa | |
let largo = 0.002; // 0.001 son 100 metros aprox | |
for (var key in points){ | |
let lista = points[key]; | |
if (lista.length > 1) { // requiere ser separado | |
// todos va a rodear al punto que les corresponde en angulos (radianes) iguales | |
let angulo = 2 * Math.PI / lista.length; | |
for (i=0; i<lista.length; i++) { | |
let feature = lista[i]; | |
feature.setProperty('hermanos', lista); // por si quiero usar despues | |
let geom = feature.getGeometry(); | |
let lat = geom.get().lat(); // -31.3382113 | |
let lng = geom.get().lng(); // -64.2942529 | |
// aca está toda la magia. Encuentra X e Y para rotar a todos los hermanos dentro de su centro | |
let new_lat = lat + Math.cos(angulo*i) * largo; | |
let new_lng = lng - Math.sin(angulo*i) * largo; | |
let old_lat_lng = new google.maps.LatLng(lat, lng); | |
let new_lat_lng = new google.maps.LatLng(new_lat, new_lng); | |
feature.setGeometry( new_lat_lng ); | |
// unir el punto con el que le corresponde a traves de una línea | |
var linea = new google.maps.Polyline({ | |
path: [old_lat_lng, new_lat_lng], // entre el original y el nuevo | |
geodesic: true, | |
strokeColor: '#FF0000', | |
strokeOpacity: 1.0, | |
strokeWeight: 2 | |
}); | |
linea.setMap(map); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment