我打算参考此代码从lat long值绘制多边形。
open layers 3 how to draw a polygon programmically?

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显示为错误。任何帮助,将不胜感激。

最佳答案

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);

关于javascript - OpenLayers.source未定义-开放图层,开放街道 map ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32565666/

10-12 13:08