简体   繁体   English

Leaflet GeoJSON Uncaught TypeError:无法读取未定义的属性(读取“x”)

[英]Leaflet GeoJSON Uncaught TypeError: Cannot read properties of undefined (reading 'x')

I have the following code working here and in jsfiddle to to add a GeoJSON LineString to a leaflet map using a function which is triggered by clicking on the map.我有以下代码在此处和 jsfiddle 中工作,以使用通过单击地图触发的函数将 GeoJSON LineString 添加到传单地图。

 // get base map tiles var map = L.map('map').setView([30, 1], 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 data = { "type": "FeatureCollection", "features": [{ "id": "0", "type": "Feature", "properties": {}, "geometry": { "type": "LineString", "coordinates": [ [ 1, 30 ], [ 2, 31 ] ] } }] } map.on('click', function(e) { L.geoJSON(data).addTo(map); });
 #map { width: 400px; height: 300px; background-color: black; }
 <script src="https://unpkg.com/leaflet@1.8.0/dist/leaflet.js"></script> <link href="https://unpkg.com/leaflet@1.8.0/dist/leaflet.css" rel="stylesheet" /> <div id="map"></div>

The issue is that I cannot get this code to work as expected in a Django application or just outside of jsfiddle.问题是我无法让这段代码在 Django 应用程序中或在 jsfiddle 之外按预期工作。 I currenlty have a html file with the following code which is included in a parent html file.我目前有一个包含以下代码的 html 文件,该代码包含在父 html 文件中。

{% load static %}
<link rel="stylesheet" type="text/css" href="{% static '/css/map.css' %}">
<link rel="stylesheet" type="text/css" href="//unpkg.com/leaflet/dist/leaflet.css">
<script src="//unpkg.com/leaflet/dist/leaflet.js"></script>

<div id="map"></div>
<script src="{% static '/js/map.js' %}"></script>

The map loads as expected but when I click on the map to activate the function, I get the following error:地图按预期加载,但是当我单击地图激活该功能时,出现以下错误:

Uncaught TypeError: Cannot read properties of undefined (reading 'x')
    at m.intersects (Bounds.js:135:27)
    at i._clipPoints (Polyline.js:251:42)
    at i._update (Polyline.js:296:8)
    at i._reset (Path.js:140:8)
    at i.onAdd (Path.js:85:8)
    at i._layerAdd (Layer.js:114:8)
    at i.whenReady (VM32 leaflet.js:5:42397)
    at i.addLayer (VM32 leaflet.js:5:65406)
    at i.eachLayer (LayerGroup.js:122:11)
    at i.onAdd (LayerGroup.js:106:8)

The issue is caused by min2 being undefined in Bounds.js.该问题是由 Bounds.js 中未定义 min2 引起的。

Any help would be very appreciated :)任何帮助将不胜感激:)

It turns out the issue was caused by the map already being initialised which led to the bounds being undefined.事实证明,问题是由于地图已经初始化导致边界未定义。 This answer solved the problem - I just needed to add this to the top of the js file:这个答案解决了这个问题 - 我只需要将它添加到 js 文件的顶部:

var container = L.DomUtil.get('map');
if (container != null) {
    container._leaflet_id = null;
}

暂无
暂无

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

相关问题 未捕获的类型错误:无法读取未定义的属性(读取“x”) - Uncaught TypeError: Cannot read properties of undefined (reading 'x') 未捕获的类型错误:无法读取未定义的属性(读取“8”) - Uncaught TypeError: Cannot read properties of undefined (reading '8') 未捕获的类型错误:无法读取未定义的属性(读取“0”) - Uncaught TypeError: Cannot read properties of undefined (reading '0') 未捕获的类型错误:无法读取未定义的属性(读取“0”) - Uncaught TypeError: Cannot read properties of undefined (reading '0') 未捕获的类型错误:无法读取未定义的属性(读取“”) - Uncaught TypeError: Cannot read properties of undefined (reading '') 问题 leaflet 和 leaflet draw vue leaflet.draw.js?20d6:8 Uncaught TypeError: Cannot read properties of undefined (reading 'length') - the problem with leaflet and leaflet draw vue leaflet.draw.js?20d6:8 Uncaught TypeError: Cannot read properties of undefined (reading 'length') React-leaflet Uncaught TypeError:无法读取未定义的属性(读取“标记”) - React-leaflet Uncaught TypeError: Cannot read properties of undefined (reading 'marker') 未捕获的类型错误:无法读取 null 的属性(正在读取“切片”)------ 未捕获的类型错误:无法读取未定义的属性(正在读取“过滤器”) - Uncaught TypeError: Cannot read properties of null (reading 'slice') ------ Uncaught TypeError: Cannot read properties of undefined (reading 'filter') Uncaught TypeError TypeError:无法读取未定义的属性(读取“路径”) - Uncaught TypeError TypeError: Cannot read properties of undefined (reading 'path') 未捕获的类型错误:无法读取未定义的属性(读取“目标”)和(读取“值”) - Uncaught TypeError: Cannot read properties of undefined (reading 'target') & (reading 'value')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM