【问题标题】:Read lat and long cordinates from a feature inside geojson file and set as center of the openlayers map on map load从 geojson 文件中的特征读取经纬度坐标,并在地图加载时设置为 openlayers 地图的中心
【发布时间】:2019-04-17 10:41:13
【问题描述】:

我是打开图层的新手,目前在读取和绘制地图中心的经纬度时遇到问题。

我在 geojson 文件中有功能 ID,即 slatslong,我可以使用 var SSlat= feature.get('sat')var SSlong =feature.get('slong') 在样式函数中调用它们来读取它们。

第一次执行时我得到了 lat long,在完整样式函数执行结束时 var SSlong 和 var SSLat 变得未定义。请让我知道有什么办法吗

var SSlat;
var SSlong;

var styleFunction = function(feature) {
  SSlat=feature.get('slat');
  SSlong=feature.get('slong');
};

const map = new Map({
  layers: [
    new TileLayer({
      source: new OSM()
    }),
    new VectorLayer({
      source: new VectorSource({
        url:'http://127.0.0.1:8080/abc.geojson',
        crossOrigin:'null',
        projection: 'EPSG:3857',
        format: new GeoJSON(), 
      }),
      style: styleFunction
    })
  ],
  target: 'map',
  view: new View({
    center: [-9752120.04, 5132251.45],// here add lat and long which needs to be taken from geojson file
    zoom: 13
  })
});

【问题讨论】:

    标签: openlayers


    【解决方案1】:

    加载要素时,我会将地图以要素的几何图形居中。如果 slat 和 slong 属性不同且更受欢迎,则可以使用它们。您应该将源分配给一个变量,以便于引用。

    import {getCenter} from 'ol/extent.js';
    
    let vectorSource = new VectorSource({
        url:'http://127.0.0.1:8080/abc.geojson',
        format: new GeoJSON(), 
      });
    
    vectorSource.on('addfeature', function(e) {
      map.getView().setCenter(getCenter(e.feature.getGeometry().getExtent()));
    });
    
    const map = new Map({
      layers: [
        new TileLayer({
          source: new OSM()
        }),
        new VectorLayer({
          source: vectorSource,
          style: styleFunction
        })
      ],
      target: 'map',
      view: new View({
        center: [-9752120.04, 5132251.45],// here add lat and long which needs to be taken from geojson file
        zoom: 13
      })
    });
    

    使用特征中的slatslong 属性居中

    vectorSource.on('addfeature', function(e) {
      var SSlat = e.feature.get('slat');
      var SSlong = e.feature.get('slong');
      if (SSlat && SSlong) {
        map.getView().setCenter(fromLonLat([SSlong, SSlat]);
      }
    });
    

    如果属性是字符串,你可能需要使用[Number(SSlong), Number(SSlat)]

    【讨论】:

    • 感谢 mike,我在 geojson 文件中有数千个纬度和经度,其中 slat 和 slog 属性帮助我将地图居中,因此可以看到其他经度和长蓬。请让我知道如何从上面的代码中获取它们并设置为中心,以及我需要放置 VectorSource.on('addfeature', function(e) { map.getView().setCenter(getCenter(e.feature. getGeometry().getExtent())); });在我的代码中
    • 您的意思是您的数千个功能中只有一个具有slatslong 属性并且您想要找到并使用它,或者这些功能是否具有不同的值并且您想要包含所有?
    • geometry 有数千个,slat 和 slong 属性各有一个值。比如 "Slat": "1234.01477" " and Slong": "-1234.89216" 需要添加到地图中心,这属性是唯一的,并且只在 geojson 文件中的特征中出现一次
    • 您只能以一个点为中心,因此不能以所有点为中心。要使视图中的所有功能都适合,您可以使用map.getView().fit(vectorSource.getExtent());
    • 我只需要在一个点而不是所有点上居中,并且该中心点存储在 slat 和 slong 中。像中心:[-123.67,34.45]。其中 slat 有 -123.67 而俚语有 34.5 请告诉我
    猜你喜欢
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-25
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多