[OpenLayers-Users] Finding the closest feature
Christopher Schmidt
crschmidt at metacarta.com
Thu Oct 8 11:22:52 EDT 2009
On Thu, Oct 08, 2009 at 10:56:55AM -0400, Jesse Bushkar wrote:
> I have a question about finding the nearest feature from any given point. I
> have a map with several featues, and I have a point somewhere on that map
> that represents my location. Is there a function I can call that will look
> at the existing features in the layer and output the marker? I'm
> envisioning something like this:
>
> var nearPoint = findClosest(myLoc);
> var dist = myLoc.distanceTo(nearPoint);
> alert(dist);
>
> I guess the way to perform that fake findClosest function is what I'm
> looking for. I'd rather not have to measure individual features, because
> these features are being dynamically added, removed, and modified on a map
> at any given time. SO, to simply shuffle through all features at the time
> of the function call would be GREAT!
var min = 1000000000000000;
var minFeat = null;
for (var i = 0; i < layer.features; i++) {
var dist = Math.sqrt(
Math.pow(myLoc.geometry.x - layer.features[i].geometry.x, 2) +
Math.pow(myLoc.geometry.y - layer.features[i].geometry.y, 2))
if (dist < min) {
minFeat = layer.features[i];
min = dist;
}
}
return minFeat;
> Thanks in advance for any help.
> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users
--
Christopher Schmidt
MetaCarta
More information about the Users
mailing list