在地图上添加标记 - OpenLayers

时间:2017-11-14 09:14:59

标签: javascript openlayers

当我添加矢量时,它们从最后一个car.position值共享相同的文本。我希望每个标记都有文字。来自console.log(position.car)的示例日志:example.js:72 1740,example.js:72 1653.如何修复此代码?

  const addMarkers = (lat, long) => {
    if(lat && lat !== 0) {
        const point = new ol.geom.Point(ol.proj.transform([lat, long], "EPSG:4326", "EPSG:3857"));
        const iconFeature = new ol.Feature({
          geometry: point
        });
        vectorSource.addFeature(iconFeature);
      }
    }
    //
    positions.forEach(position => {
      let long = position.geolocation.longitude;
      let lat = position.geolocation.latitude;
      const vectorLayer = new ol.layer.Vector({
        source: vectorSource,
        style: vectorStyle
      })
      if (long !== 0 && long !== 999.9 && long !== "" &&  long !== undefined) {
        addMarkers(long, lat);  
      }
      vectorLayer.setZIndex(5);
      map.addLayer(vectorLayer);
    });

0 个答案:

没有答案