【问题标题】:Openlayers Custom MarkerOpenlayers 自定义标记
【发布时间】:2012-12-11 09:25:22
【问题描述】:

一些信息: 我有一个主层(地图),我在点之间画线,这些点是从带有 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;
}

【问题讨论】:

    标签: openlayers marker


    【解决方案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  }}
    

    【讨论】:

    • 非常感谢!实际上,我的脚本中确实有 { style: { } }。但我出于某种原因在粘贴中更改了它。对不起!所以 null 参数成功了!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    相关资源
    最近更新 更多