简体   繁体   English

在 Mapbox GL JS 中,您可以将坐标传递给外部 GeoJSON 数据源吗?

[英]In Mapbox GL JS, can you pass coordinates to an external GeoJSON data source?

Can you pass coordinate values as variables when trying to retreive an external GeoJSON data source?尝试检索外部 GeoJSON 数据源时,可以将坐标值作为变量传递吗? Ideally I'd like to pass something like this, but it doesn't work for me.理想情况下,我想传递这样的东西,但它对我不起作用。

map.addSource('geojsonpoints', {
    type: "geojson",
    data: 'http://myexample.com/pins?lat={lat}&lon={long}'
  });

I am able to pass Z, X, Y coordinates if I use Map Vector Tiles (mvt) as a source.如果我使用 Map Vector Tiles (mvt) 作为源,我可以传递 Z、X、Y 坐标。 ie This works:即这有效:

  map.addSource('mapvectortiles', {
    'type': 'vector',
    'tiles': ['http://myexample.com/{z}/{x}/{y}'],

But I haven't figured out how to do it for a GeoJSON source.但我还没有想出如何为 GeoJSON 源做这件事。 Anyone have any ideas if it is possible in n Mapbox GL JS?如果在 n Mapbox GL JS 中有可能,有人有任何想法吗?

FYI, I am able to generate the URL using the method below, but the problem is it doesn't refresh when I move the map, unlike vector tiles.仅供参考,我可以使用以下方法生成 URL,但问题是当我移动 map 时它不会刷新,这与矢量图块不同。

var lng = map.getCenter().lng
var lat = map.getCenter().lat
var url = 'http://myexample.com/pins?lat='+lat+'&lon='+lng
map.addSource('EPC', {
  type: "geojson",
  data: url
});

I use GeoJSON to draw Tiles on the map我使用 GeoJSON 在 map 上绘制 Tiles

this is a sample GeoJSON:这是一个示例 GeoJSON:

    { "type": "FeatureCollection", 
  "features": [
    { "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [ 
            [4.342254780676343, 50.89533552689166], 
            [4.342254780676343, 50.89443721160754], 
            [4.340830581474948, 50.89443721160754],
            [4.340830581474948, 50.89533552689166],
            [4.342254780676343, 50.89533552689166]
         ]
        ]
      },
      "properties": {}
    }    
  ]
}

after all you have to add the source and add the layer毕竟你必须添加源并添加图层

add Source:添加来源:

const sourceJson: GeoJSONSourceRaw = {
        type: 'geojson',
        data: data as FeatureCollection
    };
    map.addSource(sourceId, sourceJson)

data is your json file数据是你的 json 文件

add Layer:添加图层:

 const layer: FillLayer =  {
        id: sourceId,
        source: sourceId,
        type: 'fill',
        paint: {
            'fill-color': color,
            'fill-opacity': opacity 
        }
    }
        
    this.map.addLayer(layer);

There are two parts to your question:您的问题分为两部分:

  1. Update the data source when the map is moved移动map时更新数据源
  2. Use the map's extent as part of the GeoJSON source's URL.使用地图的范围作为 GeoJSON 源 URL 的一部分。

You have part 2 under control, so:您已控制第 2 部分,因此:

Update the data source when the map is moved移动map时更新数据源

  1. Use map.on('moveend', ...使用map.on('moveend', ...
  2. Use map.getSource(...).setData(...)使用map.getSource(...).setData(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM