[英]Displaying a polygon in OpenLayers map
我想绘制一个(经度,纬度)点的多边形:
var maxPoint = [36.283, -114.368];
var geoSquare = [ minPoint, [minPoint[0], maxPoint[1]], maxPoint, [maxPoint[0], minPoint[1]]];
var polygonFeature = new Feature(
new Polygon(geoSquare));
我通过以下方式绘制地图:
var map = new Map({
interactions: defaultInteractions().extend([new Drag()]),
layers: [
new TileLayer({
source: new TileJSON({
url: 'https://maps.siemens.com/styles/osm-bright.json'
})
}),
new VectorLayer({
source: new VectorSource({
features: [polygonFeature]
}),
style: new Style({
stroke: new Stroke({
width: 3,
color: [255, 0, 0, 1]
}),
fill: new Fill({
color: [0, 0, 255, 0.6]
})
})
})
],
target: 'map',
view: new View({
center: [0, 0],
zoom: 2
})
});
这个多边形位于南加州附近,但我完全看不到地图上的正方形。 怎么了??
编辑
这是一个jsfiddle
特征坐标必须为LonLat格式,并且必须转换为视图投影。 多边形环必须在同一点开始和结束,并且多边形需要额外的一对[]
因为它们可以具有附加的内部环(孔)。 或者,您可以根据范围创建多边形。 这些都可以。
var minPoint = [-121.091, 32.92];
var maxPoint = [-114.368, 36.283];
var geoSquare = [[minPoint, [minPoint[0], maxPoint[1]], maxPoint, [maxPoint[0], minPoint[1]], minPoint]];
var polygonFeature = new Feature(
new Polygon(geoSquare).transform('EPSG:4326','EPSG:3857'));
var minPoint = [-121.091, 32.92];
var maxPoint = [-114.368, 36.283];
var polygonFeature = new Feature(
Polygon.fromExtent(minPoint.concat(maxPoint)).transform('EPSG:4326','EPSG:3857'));
var minPoint = fromLonLat([-121.091, 32.92]);
var maxPoint = fromLonLat([-114.368, 36.283]);
var geoSquare = [[minPoint, [minPoint[0], maxPoint[1]], maxPoint, [maxPoint[0], minPoint[1]], minPoint]];
var polygonFeature = new Feature(
new Polygon(geoSquare));
var minPoint = fromLonLat([-121.091, 32.92]);
var maxPoint = fromLonLat([-114.368, 36.283]);
var polygonFeature = new Feature(
Polygon.fromExtent(minPoint.concat(maxPoint)));
问题未解决?试试以下方法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.