簡體   English   中英

OpenLayers.source未定義-開放圖層,開放街道地圖

[英]OpenLayers.source is undefined - Open layers,Open Street map

我打算參考此代碼從lat long值繪制多邊形。 打開圖層3如何以編程方式繪制多邊形?

var polyCoordinates =[[1.300910900488229, 44.28372648003116],[1.3373031124022878, 44.13311552125895],[1.6648330196289067, 44.12030076099872]];
var polygon = new OpenLayers.Geometry.Polygon([polyCoordinates]);

// Create feature with polygon.
var feature = new OpenLayers.Feature(polygon);

// Create vector source and the feature to it.
var vectorSource = new OpenLayers.source.Vector();

// Create the vectorial layer
var vectorLayer = new OpenLayers.Layer.Vector('Vector Layer', {
source: vectorSource
styleMap: new OpenLayers.StyleMap({
    'default': OpenLayers.Util.applyDefaults({
            strokeWidth: 3,
            graphicName: 'triangle',
            pointRadius: '${radius}',
            rotation: '${angle}'
        }, OpenLayers.Feature.Vector.style['default']
    ),
    'select': OpenLayers.Util.applyDefaults({
            pointRadius: '${radius}'
        }, OpenLayers.Feature.Vector.style['select']
    )
})
});

未定義OpenLayers.source顯示為錯誤。 任何幫助,將不勝感激。

這是在OpenLayers 3中執行此操作的正確方法,該API具有與以前版本不同的API。

var polyCoordinates =[
    [1.300910900488229, 44.28372648003116],
    [1.3373031124022878, 44.13311552125895],
    [1.6648330196289067, 44.12030076099872]];

var polygon = new ol.geom.Polygon([polyCoordinates]);

polygon.applyTransform(ol.proj.getTransform('EPSG:4326', 'EPSG:3857'));

// Create feature with polygon.
var feature = new ol.Feature({
    geometry: polygon
});

// Create vector source and the feature to it.
var vectorSource = new ol.source.Vector({
    features: [feature]
});

// Create the vectorial layer
var vectorLayer = new ol.layer.Vector({
    source: vectorSource,
    style: new ol.style.Style({
        stroke: new ol.style.Stroke({
            color: '#eedd00',
            width: 3
        })
    })
});

編輯:如果堅持使用OpenLayers 2,則需要通過線性環創建多邊形。 設置略有不同。 沒有OpenLayers source需要處理。

var polyCoordinates =[
    [1.300910900488229, 44.28372648003116],
    [1.3373031124022878, 44.13311552125895],
    [1.6648330196289067, 44.12030076099872]];

// Create the polygon via linear ring
var points = [];

for (var i = 0; i < polyCoordinates.length; i++) {
    coord = polyCoordinates[i];
    points.push(new OpenLayers.Geometry.Point(coord[0], coord[1]));
}

var linearRing = new OpenLayers.Geometry.LinearRing(points);
var polygon = new OpenLayers.Geometry.Polygon([linearRing]);

var srcProj = new OpenLayers.Projection("EPSG:4326"); 
var targetProj = new OpenLayers.Projection("EPSG:3857");
polygon.transform(srcProj, targetProj);

// Create feature with polygon.
var feature = new OpenLayers.Feature.Vector(polygon);

// Create the vectorial layer
var vectorLayer = new OpenLayers.Layer.Vector('Vector Layer', 
    OpenLayers.Feature.Vector.style['default']   
);

vectorLayer.addFeatures([feature]);
var sitePoints = [];
var coordinates =[{"long":1.300910900488229,"lat":44.28372648003116},
                    {"long":1.3373031124022878,"lat":44.13311552125895},
                    {"long":1.6648330196289067,"lat":44.12030076099872}];

var siteStyle = {
        label:'ring',
        title: 'press to close as a ring',
        cursor: "pointer",
        fontSize: '8px',
        fontColor: '#222',
        pointRadius: 10,
        fillColor: '#cccccc',
        strokeColor: '#444444'  
};

var epsg4326 = new OpenLayers.Projection("EPSG:4326");
for (var i in coordinates) {
  var coord = coordinates[i];
  var point = new OpenLayers.Geometry.Point(coord.long, coord.lat);
  // transform from WGS 1984 to Spherical Mercator
  point.transform(epsg4326, map.getProjectionObject());
  sitePoints.push(point);
}

 console.log(sitePoints);

var linearRing = new OpenLayers.Geometry.LinearRing(sitePoints);
var geometry = new OpenLayers.Geometry.Polygon([linearRing]);
var polygonFeature = new OpenLayers.Feature.Vector(geometry, null,   siteStyle);
vectorLayer.addFeatures([polygonFeature]);
map.addLayer(vectorLayer); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM