Openlayers:矢量特征而不是标记

发布于 2024-11-08 11:42:13 字数 1276 浏览 1 评论 0原文

我想在地图上放置一个符号。例如

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

这可以正常工作并显示上面的地图。但我无法让它与 Vectors 一起使用,将最后 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 是否需要任何特殊的魔法?

I want to place a symbol o a Map. E.g.

Map Example

So far I have used OpenLayers with OpenLayers.Layer.Markers. The code looks like this:

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

This works as excepted and shows the map above. But I can't get it to work with Vectors replacing the last 3 lines with:

     vectors = new OpenLayers.Layer.Vector("Vector Layer");
     vectors.addFeatures([new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(lonLat))]);
     map.addLayer(vectors);

Is there any special magic needed to use OpenLayers.Feature.Vector?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2024-11-15 11:42:13

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

OpenLayers.Geometry.Point receives two coordinates in its constructor and not an 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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文