如何使用 openlayers 2.13 使功能适合页面绑定?
How to fit feature to page bound using openlayers2.13?
我想要 select 功能,然后使功能适合页面绑定。
我使用这个代码:
selected.geometry.resize(2, origin);
我的代码是 selected
的两倍,但我希望 selected
适合页面绑定。
我不知道如何计算 scale
并在上面的代码中放置 2
。
有人可以帮助我吗?
我找到了答案:
function fitToScale(){
minBoundX=vectors.selectedFeatures[0].geometry.getBounds().left;
minBoundY=vectors.selectedFeatures[0].geometry.getBounds().bottom;
maxBoundX=vectors.selectedFeatures[0].geometry.getBounds().right;
maxBoundY=vectors.selectedFeatures[0].geometry.getBounds().top;
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;
}
}
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);
var x=(maxBoundX+minBoundX)/2;
var y=(maxBoundY+minBoundY)/2;
origin = new OpenLayers.Geometry.Point(x,y);
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);
newLine.geometry.resize(min, origin);
vectors.removeFeatures(newLine);
vectors.redraw();
}
}
我想要 select 功能,然后使功能适合页面绑定。
我使用这个代码:
selected.geometry.resize(2, origin);
我的代码是 selected
的两倍,但我希望 selected
适合页面绑定。
我不知道如何计算 scale
并在上面的代码中放置 2
。
有人可以帮助我吗?
我找到了答案:
function fitToScale(){
minBoundX=vectors.selectedFeatures[0].geometry.getBounds().left;
minBoundY=vectors.selectedFeatures[0].geometry.getBounds().bottom;
maxBoundX=vectors.selectedFeatures[0].geometry.getBounds().right;
maxBoundY=vectors.selectedFeatures[0].geometry.getBounds().top;
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;
}
}
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);
var x=(maxBoundX+minBoundX)/2;
var y=(maxBoundY+minBoundY)/2;
origin = new OpenLayers.Geometry.Point(x,y);
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);
newLine.geometry.resize(min, origin);
vectors.removeFeatures(newLine);
vectors.redraw();
}
}