0

一些信息:我有一个主层(地图),我在点之间画线,这些点是从带有 Linestring 的 JSON 结果中接收的。

(问题)我在网上遵循了一个关于如何自定义我要添加的点的示例。但这不起作用。(看底部的功能。)

代码:

//'listOfPoints' is an array containing all the point objects.

var pointmap = new OpenLayers.Geometry.LineString(listOfPoints);

    var lastpoint = listOfPoints[listOfPoints.length -1];


    var vesselLayer = new OpenLayers.Layer.Vector(data.bridge_name);

    if (lastpoint != null) {
        var markerLayer = getPOI(lastpoint);
        vesselLayer.addFeatures([pointmap,markerLayer]);
    } else {
        vesselLayer.addFeatures([new OpenLayers.Feature.Vector(pointmap)]);
    }

// Function for creating a marker and returning it to the caller. 

function getPOI(point) {

//This was also tried without the "Style" property. (Only the externalGraphic line)
var temp_feature = new OpenLayers.Feature.Vector(
        point,
        style: { externalGraphic: '/assets/img/marker.png', graphicHeight: 16,     graphicWidth: 16, graphicXOffset:8, graphicYOffset:8  }
    );    

return temp_feature;
}
4

1 回答 1

1

乍一看,temp_feature 定义有错误:

var temp_feature = new OpenLayers.Feature.Vector(
    point,
    null,
    {
        externalGraphic: '/assets/img/marker.png',
        graphicHeight: 16,
        graphicWidth: 16,
        graphicXOffset:8,
        graphicYOffset:8
    }
); 

阅读和遵循 API 文档很有用: http ://dev.openlayers.org/docs/files/OpenLayers/Feature/Vector-js.html#OpenLayers.Feature.Vector.OpenLayers.Feature.Vector

OpenLayers.Feature.Vector 采用三个参数,几何,具有属性的对象(在您的情况下为空,因为您没有任何属性)和具有样式的对象。

我还没有测试过那个代码,可能还有其他问题。

关于 JavaScript 的一般用途,从来没有这样的事情

style: { externalGraphic: '/assets/img/marker.png', graphicHeight: 16,     graphicWidth: 16, graphicXOffset:8, graphicYOffset:8  }

对象总是在括号内定义:

{style: { externalGraphic: '/assets/img/marker.png', graphicHeight: 16,     graphicWidth: 16, graphicXOffset:8, graphicYOffset:8  }}
于 2012-12-11T09:33:53.867 回答