Openlayers:矢量特征而不是标记

时间:2011-05-17 04:59:18

标签: openlayers

我想在地图上放置一个符号。 E.g。

Map Example

到目前为止,我已将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需要特殊魔法吗?

1 个答案:

答案 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);