I want to implement snap to Marker functionality while dragging another Marker.
I have a function that checks if two markers are ‘close’: (got it from here)
function arePointsNear(point1, point2) {
var sw = new google.maps.LatLng(point2.lat() - 0.005, point2.lng() - 0.005);
var ne = new google.maps.LatLng(point2.lat() + 0.005, point2.lng() + 0.005);
var bounds = new google.maps.LatLngBounds(sw, ne);
return bounds.contains(point1);
};
Now, on drag event of a marker, i do like this:
for (var index in allMarkers) {
if(allMarkers[index] == marker) {
continue;
}
var point1 = allMarkers[index].position;
var point2 = marker.position;
// This always returns true
var isClose = arePointsNear(point1, point2);
}
Am i doing something wrong?
The algorithm is working..the problem was inside my code. I was checking the positions of the same marker, not of two markers.