2015-11-02 56 views

回答

6

对于带有geometry function的标签,您可以使用单独的样式,该标签将为标签位置返回单个点。

var styles = [ 
    // Style for the label 
    new ol.style.Style({ 
    text: new ol.style.Text({..}), 
    geometry: function(feature) { 
     // expecting a MultiPolygon here 
     var interiorPoints = feature.getGeometry().getInteriorPoints(); 
     return interiorPoints.getPoint(0); 
    } 
    }), 
    // Style for the polygons 
    new ol.style.Style({ 
    stroke: new ol.style.Stroke({...}), 
    fill: new ol.style.Fill({...}) 
    }) 
]; 

http://jsfiddle.net/p0acpbtg/2/

+0

真是太好了! '+ 1' ...因此不喜欢'+ 1'。 –

+0

工程就像一个魅力!真棒。 – Enzo

+0

不适合我。它集中在多面体内的一个多边形上。如果我将它更改为'return interiorPoints.getPoint(1);'它以另一个多边形为中心。我怎样才能让它集中在多面体上? –