问题描述
一些信息:我得到了一个主层(地图),并且我用Linestring在从JSON结果中获取的点之间绘制了线.
some info:I got a main layer (map) which and im drawing lines between points recived from a JSON result with a Linestring.
(问题)我在网上跟随一个示例,说明如何自定义要添加的点.但这是行不通的.(查看底部的功能.)
(issue)I followed an example online on how to customize the points i am adding. But this does not work. (Look at the function on the bottom.)
代码:
//'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;
}
推荐答案
乍看之下,temp_feature定义有误:
At first glance, there's error in temp_feature definition:
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
It's useful to read and follow API documentation:http://dev.openlayers.org/docs/files/OpenLayers/Feature/Vector-js.html#OpenLayers.Feature.Vector.OpenLayers.Feature.Vector
OpenLayers.Feature.Vector接受三个参数,即几何图形,具有属性的对象(在您的情况下为null,因为您没有任何属性)和具有样式的对象.
OpenLayers.Feature.Vector takes three arguments, geometry, object with attributes (which is null in your case, since you don't have any attributes), and object with style.
我还没有测试过该代码,可能还会有其他问题.
I haven't tested that code, there may be other problems, too.
关于JavaScript的一般用法,从来没有这样的东西
And about general use of JavaScript, there is never such thing as
style: { externalGraphic: '/assets/img/marker.png', graphicHeight: 16, graphicWidth: 16, graphicXOffset:8, graphicYOffset:8 }
对象总是在方括号内定义:
Object is always defined within brackets:
{style: { externalGraphic: '/assets/img/marker.png', graphicHeight: 16, graphicWidth: 16, graphicXOffset:8, graphicYOffset:8 }}
这篇关于Openlayers自定义标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!