Openlayers Custom Marker

时间:2012-12-11 09:25:22

标签: openlayers marker

一些信息: 我得到了一个主图层(地图),它是从JSON结果中使用Linestring重新获得的点之间的线条。

(问题) 我在网上关注如何自定义我添加的点的示例。但这不起作用。 (查看底部的功能。)

代码:

//'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;
}

1 个答案:

答案 0 :(得分: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有三个参数,几何,带有属性的对象(在你的情况下为null,因为你没有任何属性),以及带有样式的对象。

我没有测试过该代码,也可能存在其他问题。

关于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  }}