我想在地图上放置一个符号。 E.g。
到目前为止,我已将OpenLayers与OpenLayers.Layer.Markers
一起使用。代码如下所示:
map = new OpenLayers.Map('map');
layer = new OpenLayers.Layer.OSM( "Simple OSM Map");
map.addLayer(layer);
map.setCenter(
new OpenLayers.LonLat({{ location.lon }}, {{ location.lat }}).transform(
new OpenLayers.Projection("EPSG:4326"),
map.getProjectionObject()
), 15);
var lonLat = new OpenLayers.LonLat({{ location.lon }}, {{ location.lat }})
.transform(
new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
map.getProjectionObject() // to Spherical Mercator Projection
);
var markers = new OpenLayers.Layer.Markers( "Markers" );
map.addLayer(markers);
markers.addMarker(new OpenLayers.Marker(lonLat));
这可以作为例外,并显示上面的地图。但是我无法使用Vector替换最后3行:
vectors = new OpenLayers.Layer.Vector("Vector Layer");
vectors.addFeatures([new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(lonLat))]);
map.addLayer(vectors);
使用OpenLayers.Feature.Vector
需要特殊魔法吗?
答案 0 :(得分:21)
OpenLayers.Geometry.Point
在其构造函数中接收两个坐标,而不是OpenLayers.LonLat
。
vectors = new OpenLayers.Layer.Vector("Vector Layer");
point = new OpenLayers.Geometry.Point(lonLat.lon, lonLat.lat);
vectors.addFeatures([new OpenLayers.Feature.Vector(point)]);
map.addLayer(vectors);