Here’s a quick one that was inspired by a StackOverflow question and built using the references posted here. It allows you to compute the position of an arbitrary number of points (expressed as latitude/longitude pairs) equidistant from a given centerpoint (using a given radius/distance, of course) on the surface of the Earth. The crux of the code goes like so:
window.pointsAround = function(centerLat, centerLng, radius, numInterpolations) {
var result = [];
//do annoying trig maths to work out the delta in latitude between our start and end points
var targetD = radius; //km
var fractionalDistance = (targetD / radiusOfEarth) / 2;
var sqrtA = Math.sin(fractionalDistance);
var a = sqrtA * sqrtA;
var sinHalfDLat = Math.sqrt(a);
var dLat = Math.asin(sinHalfDLat) * 2;
var dLatDegrees = degrees(dLat);
var minLat = centerLat - dLatDegrees; //furthest valid latitude above the origin
var maxLat = centerLat + dLatDegrees; //furthest valid latitude below the origin
var centerLatRadians = radians(centerLat);
//topmost and bottommost points in the circle
result.push({lat: minLat, lng: centerLng});
result.push({lat: maxLat, lng: centerLng});
//step from minLat to maxLat, interpolating coordinates that lie upon the circle
numInterpolations = numInterpolations ? numInterpolations : 360;
var step = (maxLat - minLat) / (numInterpolations / 2.0);
for (var count = 0; count < (numInterpolations / 2) - 1; count++) {
minLat += step;
var minLatRadians = radians(minLat);
dLat = radians(centerLat - minLat);
//more annoying trig to work out the delta in longitude for our interpolated coordinate
var dLon = 2 * Math.asin(Math.sqrt((a - (Math.sin(dLat/2) * Math.sin(dLat/2))) / (Math.cos(minLatRadians) * Math.cos(centerLatRadians))));
var dLonDegrees = degrees(dLon);
var newLng = centerLng + dLonDegrees;
var deltaLng = newLng - centerLng;
result.push({lat: minLat, lng: newLng});
result.push({lat: minLat, lng: centerLng - deltaLng});
}
return result;
};
This code works out the northernmost and southernmost points that are the specified distance away from the given centerpoint. It then steps between the minimum and maximum latitude of these two points, interpolating the appropriate longitudinal value(s) to satisfy the equidistant constraint. The total number of coordinates returned can be controlled using the optional ‘numInterpolations‘ parameter. When not specified a total of 360 points will be returned by default, because that is a nice, round number.
If you use something like the following code to plot some points on a map:
window.map = new GMap2(document.getElementById("mapDiv"));
var centerPoint = new GLatLng(37.4419, -122.1419);
map.setCenter(centerPoint, 8);
map.addControl(new GLargeMapControl3D());
map.addControl(new GMapTypeControl());
var marker = new GMarker(centerPoint);
var markerText = "Center @ 37.4419, -122.1419";
GEvent.addListener(marker, "click", markerClickHandler(marker, markerText));
map.addOverlay(marker);
var points = pointsAround(37.4419, -122.1419, 50);
for (var index = 0; index < points.length; index++) {
var point = points[index];
var latlng = new GLatLng(point.lat, point.lng);
marker = new GMarker(latlng);
markerText = "Marker @ " + point.lat + ", " + point.lng;
GEvent.addListener(marker, "click", markerClickHandler(marker, markerText));
map.addOverlay(marker);
}
…you will get a nice circle of points around the hard-coded centerpoint. You can see a working example of this here: http://jsfiddle.net/HmchC/27/.
Hi Aroth,
I am working on crawler and have some doubts
I found you are one of the most knowledgeable people on net whom I can ask
I could find link on this blog, can I send you email
Thanks