2

I have to change vector feature when vectors are intersect each other, I wrote the following code:

// check all vectors features (arrows) in the current layer for intersections
  allArrowFeatures = pointsLayer.features;
  var hideArr = new Array();
  var l = 0;
  for(var i = 0 ;i < allArrowFeatures.length;i++){

    arrowObj = allArrowFeatures[i];
    for(var j = 1 ;j < allArrowFeatures.length;j++){
      if(j===i){ // same vector
        continue;
      }
      if(allArrowFeatures[j].geometry.bounds.intersectsBounds(arrowObj.geometry.bounds)){
        hideArr[l] = pointsLayer.features[i].style = {display:'none'};
        l++;
      }

    }
  }

When you zooming in to the max zoom you wont see any intersections so I assume this code is OK but the problem is when I zooming out the vectors still intersects each other. zoom out example zoom in example

Its look like I don't use the correct function for my case, and I tried several ways this was the closest to my case. Any idea what openlayers method I have to use to detect if there was intersection also in zoom out probably not base on the bounds?

1 Answer 1

1

Ok, after deep research I realize I was doing it completely wrong instead of create my own detect intersections function I had to use the following openlayers framework class:

OpenLayers.Strategy.Cluster

It doing exactly what I wanted to achieve by detecting the intersections, and in much more efficient way, it wont draw vectors who intersect and draw instead the intersect icon, if you want to use it as is it will be easy:

// distance: is the distance in pixel in for merging the vectors
// threshold: is the minimum number of points for clustering 
  var strategy = new OpenLayers.Strategy.Cluster({distance: 15, threshold: 3});

// creating the vector layer and adding the strategy to the vector layer
  var pointsLayer = new OpenLayers.Layer.Vector("Points", {strategies: [strategy]});

but if you want to start customizing the icons and behaviors it will be quit mess, I found several posts who made my work more easy:

clustring with animations

openlayers example

Customized OpenLayers cluster strategies

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.