2016-08-15 4 views
3

私はユーザーが世界地図上の領域をマークすることができるアプリケーションを書いています。これらの領域は非常に小さくても構いません。スタイルの長方形の最小サイズ

(スタイル機能などで)どのように(矩形の)フィーチャが常にレンダリングされるべきかを定義する方法はありますか少なくとも、例えば10px×10px?

アップデート:私は現在使用していくつかのコード:

描画側:

vectorSource.addFeature(
    new ol.Feature({ 
    geometry: ol.geom.Polygon.fromExtent(app.map.lonlate(extent)), 
    // … some more custom properties like a display name … 
    }) 
); 

スタイル機能:

function(feature) { 
    return [new ol.style.Style({ 
    stroke: new ol.style.Stroke({ 
     color: feature.get('mine') ? '#204a87' : '#729fcf', 
     width: 2 
    }), 
    fill: new ol.style.Fill({ 
     color: 'rgba(255, 255, 255, ' + (feature.get('mine') ? '0.5' : '0.2') + ')' 
    }) 
    })]; 
} 
var draw = new ol.interaction.Draw({ 
    source: vectorSource, 
    type: 'LineString', 
    geometryFunction: function(coordinates, geometry) { 
    if(!geometry) { 
     geometry = new ol.geom.Polygon(null); 
    } 
    var start = coordinates[0]; 
    var end = coordinates[1]; 
    geometry.setCoordinates([[ 
     start, 
     [start[0], end[1]], 
     end, 
     [end[0], start[1]], 
     start 
    ]]); 
    return geometry; 
    }, 
    maxPoints: 2 
}); 

draw.on('drawend', function(e) { 
    var extent = e.feature.getGeometry().getExtent(); 
    extent = app.map.rlonlate(extent); // own function to convert it from map coordinates into lat/lon 
    // some code to save the extent to the database 
}); 

と表示側の

+0

:現在の解像度で10ピクセル。 –

+0

@ JonatasWalkerまあ、描画するときではなく、(世界の)地図に四角形を表示するときです。ベルリンの周りに長方形を作成し、それを世界地図に表示してみましょう。そこには1px×1pxのサイズ(+ボーダーの幅)があります – levu

+0

しかし、ワークフローはどうですか?ユーザーが長方形を作成してGeoJSONに保存するユーザーがいますか? –

答えて

1

Usinスタイル関数は良い考えです。スタイルファンクションの2番目の引数はresolutionです。これを使用して、フィーチャのジオメトリが、たとえば、より小さいかどうかを確認できます。あなたはいくつかの関連するコード、特別 `ol.interaction.Draw`を示すことができ

var styleFn = function(feature, resolution) { 
    var minSizePx = 30; 
    var minSize = minSizePx * resolution; 
    var extent = feature.getGeometry().getExtent(); 

    if (ol.extent.getWidth(extent) < minSize || ol.extent.getHeight(extent) < minSize) { 
    // special style for polygons that are too small 
    var center = new ol.geom.Point(ol.extent.getCenter(extent)); 
    return new ol.style.Style({ 
     geometry: center, 
     image: ... 
    } else { 
    // normal style 
    return defaultStyle; 
    } 
}; 

http://jsfiddle.net/ukc0nmy2/1/

+0

ありがとうございました! – levu