简体   繁体   English

Leaflet.js 和使用 geoJSON 加载的表单文件

[英]Leaflet.js and working with geoJSON loaded form file

After creating a map using leaflet.js and then loading a geoJSON layer, I am trying to add a popup to each feature when clicked - the examples I can find all assume the geoJSON added inline, but I load mine from a file (which I would have thought more common, but anyway..) and I can't get it to work.在使用 leaflet.js 创建 map 然后加载 geoJSON 图层后,我试图在单击时向每个功能添加一个弹出窗口 - 我可以找到的示例都假设 geoJSON 是内联添加的,但我从文件加载我的(我本来会认为更常见,但无论如何..)我无法让它工作。

I know the geoJSON is OK, because I have this working using Google Maps, but I want to try and do this using Leaflet.js我知道geoJSON没问题,因为我使用谷歌地图进行这项工作,但我想尝试使用 Leaflet.js

The below will load the geoJSON layer if I don't attempt the "onEachFeature" call如果我不尝试“onEachFeature”调用,下面将加载 geoJSON 层

var map = L.map('map').setView([55.505, -0.09], 7);

L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
  attribution: '&copy; <a href=”http://osm.org/copyright”>OpenStreetMap</a> contributors'
}).addTo(map);

var geojsonLayer = new L.GeoJSON.AJAX('[path-to-geoJSON-file]');

function onEachFeature(feature, layer) {
  if (feature.properties && feature.properties.name) {
    layer.bindPopup(feature.properties.name);
  }
}

L.geoJSON(geojsonLayer, {
  onEachFeature: onEachFeature
}).addTo(map);

//  commewnt out above block and the below works to just load the layer
geojsonLayer.addTo(map);

[edit] as Grzegorz T. has kindly pointed out, this "looks correct as much as I can say", and indeed it does work if I load the geoJSON directly into the geojsonLayer var, either via code-behind or a vanilla ajax call, so the issue with the above is clearly something to do with the GeoJSON.AJAX plugin, which is somehow not loading the geoJSON in quite the way the rest of leaflet expects. [编辑] 正如 Grzegorz T. 友好地指出的那样,这“看起来很正确”,如果我通过代码隐藏或香草 ajax 调用将geoJSON直接加载到geojsonLayer var中,它确实有效,因此上述问题显然与 GeoJSON.AJAX 插件有关,该插件在某种程度上没有以 Z1F6F5327375DE395359CC797E690657 的 rest 的方式加载 geoJSON。 If anyone has any ideas I'd be nterested, but am happy enough not using the plugin.如果有人有任何想法,我会很感兴趣,但很高兴不使用该插件。

Yet the documentation has not been read.然而文档尚未被阅读。 Not only the leaflet but also L.GeoJSON.Ajax had to be looked into.不仅需要调查 leaflet,还需要调查 L.GeoJSON.Ajax。 Not only is it described nicely and there are examples, laziness;)它不仅描述得很好,而且还有例子,懒惰;)

Just change nazwa to name because i used a different geojson file只需将nazwa更改为name ,因为我使用了不同的 geojson 文件

var map = L.map('map').setView([52.22977, 21.01178], 7);

L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
  attribution: '&copy; <a href=”http://osm.org/copyright”>OpenStreetMap</a> contributors'
}).addTo(map);

function onEachFeature(feature, layer) {
  if (feature.properties && feature.properties.nazwa) {
    layer.bindPopup(feature.properties.nazwa);
  }
}

const geojson = 'https://raw.githubusercontent.com/ppatrzyk/polska-geojson/master/wojewodztwa/wojewodztwa-min.geojson';

let jsonTest = new L.GeoJSON.AJAX(geojson, {
  onEachFeature: onEachFeature
}).addTo(map);

If you do not use jquery and you do not need to run this code on IE, I recommend another solution, ie using fetch, take a look at my example如果您不使用 jquery 并且您不需要在 IE 上运行此代码,我推荐另一种解决方案,即使用 fetch,看看我的 示例

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

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