如何将标记添加到开放层地图?

时间:2019-04-22 04:33:48

标签: openlayers

我想使用图书馆开放层,使用OpenStreetMap将地图显示到我的网站。 当我想添加标记时,我想添加一个标记,但是该标记未显示在地图上。

我在Google上进行搜索,发现下面的代码之一。 有很多添加标记的示例,但它在Google地图中占主导地位,我想使用开源地图

 <!doctype html>
 <html lang="en">
   <head>
     <link rel="stylesheet" 
 href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/ css/ol.css" type="text/css">
     <style>
       .map {
        height: 400px;
        width: 100%;
      }
    </style>
    <script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>
    <title>OpenLayers example</title>
  </head>
  <body>
    <h2>My Map</h2>
    <div id="map" class="map"></div>
    <script type="text/javascript">

///SHOW MAP
      var map = new ol.Map({
        target: 'map',
        layers: [
          new ol.layer.Tile({
            source: new ol.source.OSM()
          })
        ],
        view: new ol.View({
          center: ol.proj.fromLonLat([106.8478695, -6.1568562]),
          zoom: 17
        })
      });
///ADD MARKER
    var markers = new OpenLayers.Layer.Markers( "Markers" );
    map.addLayer(markers);

    var size = new OpenLayers.Size(21,25);
    var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
    var icon = new OpenLayers.Icon('marker.png', size, offset);
    markers.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(106.8478695,-6.1568562),icon));
    markers.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(106.8478695,-6.1568562),icon.clone()));

    </script>
    </body>
    </html>

代码段:

///SHOW MAP
var map = new ol.Map({
  target: 'map',
  layers: [
    new ol.layer.Tile({
      source: new ol.source.OSM()
    })
  ],
  view: new ol.View({
    center: ol.proj.fromLonLat([106.8478695, -6.1568562]),
    zoom: 17
  })
});
///ADD MARKER
var markers = new OpenLayers.Layer.Markers("Markers");
map.addLayer(markers);

var size = new OpenLayers.Size(21, 25);
var offset = new OpenLayers.Pixel(-(size.w / 2), -size.h);
var icon = new OpenLayers.Icon('marker.png', size, offset);
markers.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(106.8478695, -6.1568562), icon));
markers.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(106.8478695, -6.1568562), icon.clone()));
.map {
  height: 400px;
  width: 100%;
}
<link rel="stylesheet" href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/ css/ol.css" type="text/css">
<script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>
<title>OpenLayers example</title>
<h2>My Map</h2>
<div id="map" class="map"></div>

我希望标记会显示在地图上,但我看不到标记

1 个答案:

答案 0 :(得分:1)

您正在使用OpenLayers 2代码将标记添加到OpenLayers 5地图。

OpenLayers 3/4/5的等效代码为

var markers = new ol.layer.Vector({
  source: new ol.source.Vector(),
  style: new ol.style.Style({
    image: new ol.style.Icon({
      anchor: [0.5, 1],
      src: 'marker.png'
    })
  })
});
map.addLayer(markers);

var marker = new ol.Feature(new ol.geom.Point(ol.proj.fromLonLat([106.8478695, -6.1568562])));
markers.getSource().addFeature(marker);