1

I want to select feature and then fit feature to page bound.

I use this code:

selected.geometry.resize(2, origin);

my code, double the selected, but i want fit selected to page bound. I do not know how to calculate scale and put in place 2 in the above code.

Can someone help me?

1 Answer 1

1

I found the answer:

function fitToScale(){

    // get min , max bound of selected features;

    minBoundX=vectors.selectedFeatures[0].geometry.getBounds().left;
    minBoundY=vectors.selectedFeatures[0].geometry.getBounds().top;
    maxBoundX=vectors.selectedFeatures[0].geometry.getBounds().right;
    maxBoundY=vectors.selectedFeatures[0].geometry.getBounds().bottom;
    for(i=0 ; i < vectors.selectedFeatures.length ; i++){
        gBound = vectors.selectedFeatures[i].geometry.getBounds();
        if(minBoundX>gBound.left){
            minBoundX=gBound.left;
        }
        if(minBoundY>gBound.bottom){
            minBoundY=gBound.bottom;
        }
        if(maxBoundX<gBound.right){
            maxBoundX=gBound.right;
        }
        if(maxBoundY<gBound.top){
            maxBoundY=gBound.top;
        }
    }

    // get page bound , ratio of page bound and features bound
    var mBound = map.getExtent();
    var dx1=maxBoundX-minBoundX;
    var dy1=maxBoundY-minBoundY;
    var dx2=mBound.right-mBound.left;
    var dy2=mBound.top-mBound.bottom;
    var area1=dx2/dx1;
    var area2=dy2/dy1;
    var min;

    if(area1<area2){
        min=area1;
    } else{
        min=area2;
    }
    console.log(min);

    // claculate center of features and add center point to features
    var x=(maxBoundX+minBoundX)/2;
    var y=(maxBoundY+minBoundY)/2;
    origin = new OpenLayers.Geometry.Point(x,y);
    // create new line with center point
    for(i=0;i<vectors.selectedFeatures.length;i++ ){
        selected = vectors.selectedFeatures[i];
        pLine=[];
        for(j=0;j<selected.geometry.getVertices().length;j++ ){
            pLine.push(selected.geometry.getVertices()[j]);
        }
        pLine.push(origin);
        newLine=new OpenLayers.Feature.Vector(new OpenLayers.Geometry.LineString(pLine));
        vectors.addFeatures(newLine);
        // resize and fit selected features to page bound 
        newLine.geometry.resize(min, origin);
        vectors.removeFeatures(newLine);
        vectors.redraw();
    }

}

I hope it can help you.

0

Your Answer

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

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