leaflet加载geoJSON图层数据时报Invalid LatLng object
一、问题leaflet加载geoJSON图层数据时报:Uncaught Error:Invalid LatLng object错误,如下所示
(1)Uncaught Error: Invalid LatLng object: (undefined, undefined)
(2)Uncaught Error: Invalid LatLng object: (121,39, 123,41)
二、经测试排查问题原因为geometry对象中type属性与coordinates属性不一致引起。
(1)报错代码"geometry":{ "type": "MultiPolygon", "coordinates": [[[123,41],[121,39],[126,41]]]}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Leaflet Map</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A==" crossorigin="" />
<style type="text/css">
body{
margin: 0;
padding: 0;
}
#mapDiv {
width:100%;
height:600px;
}
</style>
</head>
<body>
<!-- 创建一个 地图的div id 必须有 但是自定义 -->
<div id="mapDiv"></div>
<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"
integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA=="
crossorigin=""></script>
<script>
//用的腾讯地图的瓦片
var url = 'http://rt0.map.gtimg.com/realtimerender?z={z}&x={x}&y={-y}&type=vector&style=0';
//初始化 地图
var leafletMap = L.map('mapDiv').setView([40, 123], 5);
//将图层加载到地图上,并设置最大的聚焦还有map样式
L.tileLayer(url, {
maxZoom: 18,
id: 'mapbox.streets'
}).addTo(leafletMap);
//增加一个marker ,地图上的标记,并绑定了一个popup,默认打开
L.marker([41, 123]).addTo(leafletMap);//.bindPopup("<b>Hello world!</b><br />I am a popup.").openPopup();
//增加一个圈,设置圆心、半径、样式
L.circle([41, 123], 100000, {
color: 'red',
fillColor: '#f03',
fillOpacity: 0.5
}).addTo(leafletMap).bindPopup("I am a circle.");
//增加多边形
L.polygon([
[41, 113],
[39, 111],
[41, 116]
]).addTo(leafletMap).bindPopup("I am a polygon.");
//为点击地图的事件 增加popup
var popup = L.popup();
function onMapClick(e) {
popup
.setLatLng(e.latlng)
.setContent("You clicked the map at " + e.latlng.toString())
.openOn(leafletMap);
}
leafletMap.on('click', onMapClick);
//通过json添加图层MultiPolygon
var testData={
"type": "FeatureCollection",
"name": "TestData",
"features": [
{"type":"Feature","properties":{"fillOpacity":0.8,"fill":"rgb(163,224,142)","value":"11"},"geometry":{ "type": "MultiPolygon", "coordinates": [[[123,41],[121,39],[126,41]]]},"id":0}
]
};
var testLayer = L.geoJSON(testData, {
style: function (feature) {
return {color: '#4264fb', fillColor:feature.properties.fill, weight:1, fillOpacity:feature.properties.fillOpacity};
}
}).addTo(leafletMap).bindPopup("I am a polygon layer.");
</script>
</body>
</html>
修改为"geometry":{ "type": "MultiPolygon", "coordinates": [[[[123,41],[121,39],[126,41]]]]}后正常
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Leaflet Map</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A==" crossorigin="" />
<style type="text/css">
body{
margin: 0;
padding: 0;
}
#mapDiv {
width:100%;
height:600px;
}
</style>
</head>
<body>
<!-- 创建一个 地图的div id 必须有 但是自定义 -->
<div id="mapDiv"></div>
<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"
integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA=="
crossorigin=""></script>
<script>
//用的腾讯地图的瓦片
var url = 'http://rt0.map.gtimg.com/realtimerender?z={z}&x={x}&y={-y}&type=vector&style=0';
//初始化 地图
var leafletMap = L.map('mapDiv').setView([40, 123], 5);
//将图层加载到地图上,并设置最大的聚焦还有map样式
L.tileLayer(url, {
maxZoom: 18,
id: 'mapbox.streets'
}).addTo(leafletMap);
//增加一个marker ,地图上的标记,并绑定了一个popup,默认打开
L.marker([41, 123]).addTo(leafletMap);//.bindPopup("<b>Hello world!</b><br />I am a popup.").openPopup();
//增加一个圈,设置圆心、半径、样式
L.circle([41, 123], 100000, {
color: 'red',
fillColor: '#f03',
fillOpacity: 0.5
}).addTo(leafletMap).bindPopup("I am a circle.");
//增加多边形
L.polygon([
[41, 113],
[39, 111],
[41, 116]
]).addTo(leafletMap).bindPopup("I am a polygon.");
//为点击地图的事件 增加popup
var popup = L.popup();
function onMapClick(e) {
popup
.setLatLng(e.latlng)
.setContent("You clicked the map at " + e.latlng.toString())
.openOn(leafletMap);
}
leafletMap.on('click', onMapClick);
//通过json添加图层MultiPolygon
var testData={
"type": "FeatureCollection",
"name": "TestData",
"features": [
{"type":"Feature","properties":{"fillOpacity":0.8,"fill":"rgb(163,224,142)","value":"11"},"geometry":{ "type": "MultiPolygon", "coordinates": [[[[123,41],[121,39],[126,41]]]]},"id":0}
]
};
var testLayer = L.geoJSON(testData, {
style: function (feature) {
return {color: '#4264fb', fillColor:feature.properties.fill, weight:1, fillOpacity:feature.properties.fillOpacity};
}
}).addTo(leafletMap).bindPopup("I am a polygon layer.");
</script>
</body>
</html>
(1)报错代码"geometry":{ "type": "Polygon", "coordinates": [[[[123,41],[121,39],[126,41]]]]}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Leaflet Map</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A==" crossorigin="" />
<style type="text/css">
body{
margin: 0;
padding: 0;
}
#mapDiv {
width:100%;
height:600px;
}
</style>
</head>
<body>
<!-- 创建一个 地图的div id 必须有 但是自定义 -->
<div id="mapDiv"></div>
<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"
integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA=="
crossorigin=""></script>
<script>
//用的腾讯地图的瓦片
var url = 'http://rt0.map.gtimg.com/realtimerender?z={z}&x={x}&y={-y}&type=vector&style=0';
//初始化 地图
var leafletMap = L.map('mapDiv').setView([40, 123], 5);
//将图层加载到地图上,并设置最大的聚焦还有map样式
L.tileLayer(url, {
maxZoom: 18,
id: 'mapbox.streets'
}).addTo(leafletMap);
//增加一个marker ,地图上的标记,并绑定了一个popup,默认打开
L.marker([41, 123]).addTo(leafletMap);//.bindPopup("<b>Hello world!</b><br />I am a popup.").openPopup();
//增加一个圈,设置圆心、半径、样式
L.circle([41, 123], 100000, {
color: 'red',
fillColor: '#f03',
fillOpacity: 0.5
}).addTo(leafletMap).bindPopup("I am a circle.");
//增加多边形
L.polygon([
[41, 113],
[39, 111],
[41, 116]
]).addTo(leafletMap).bindPopup("I am a polygon.");
//为点击地图的事件 增加popup
var popup = L.popup();
function onMapClick(e) {
popup
.setLatLng(e.latlng)
.setContent("You clicked the map at " + e.latlng.toString())
.openOn(leafletMap);
}
leafletMap.on('click', onMapClick);
//通过json添加图层MultiPolygon
var testData={
"type": "FeatureCollection",
"name": "TestData",
"features": [
{"type":"Feature","properties":{"fillOpacity":0.8,"fill":"rgb(163,224,142)","value":"11"},"geometry":{ "type": "Polygon", "coordinates": [[[[123,41],[121,39],[126,41]]]]},"id":0}
]
};
var testLayer = L.geoJSON(testData, {
style: function (feature) {
return {color: '#4264fb', fillColor:feature.properties.fill, weight:1, fillOpacity:feature.properties.fillOpacity};
}
}).addTo(leafletMap).bindPopup("I am a polygon layer.");
</script>
</body>
</html>
修改为"geometry":{ "type": "Polygon", "coordinates": [[[123,41],[121,39],[126,41]]]}后正常
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Leaflet Map</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A==" crossorigin="" />
<style type="text/css">
body{
margin: 0;
padding: 0;
}
#mapDiv {
width:100%;
height:600px;
}
</style>
</head>
<body>
<!-- 创建一个 地图的div id 必须有 但是自定义 -->
<div id="mapDiv"></div>
<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"
integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA=="
crossorigin=""></script>
<script>
//用的腾讯地图的瓦片
var url = 'http://rt0.map.gtimg.com/realtimerender?z={z}&x={x}&y={-y}&type=vector&style=0';
//初始化 地图
var leafletMap = L.map('mapDiv').setView([40, 123], 5);
//将图层加载到地图上,并设置最大的聚焦还有map样式
L.tileLayer(url, {
maxZoom: 18,
id: 'mapbox.streets'
}).addTo(leafletMap);
//增加一个marker ,地图上的标记,并绑定了一个popup,默认打开
L.marker([41, 123]).addTo(leafletMap);//.bindPopup("<b>Hello world!</b><br />I am a popup.").openPopup();
//增加一个圈,设置圆心、半径、样式
L.circle([41, 123], 100000, {
color: 'red',
fillColor: '#f03',
fillOpacity: 0.5
}).addTo(leafletMap).bindPopup("I am a circle.");
//增加多边形
L.polygon([
[41, 113],
[39, 111],
[41, 116]
]).addTo(leafletMap).bindPopup("I am a polygon.");
//为点击地图的事件 增加popup
var popup = L.popup();
function onMapClick(e) {
popup
.setLatLng(e.latlng)
.setContent("You clicked the map at " + e.latlng.toString())
.openOn(leafletMap);
}
leafletMap.on('click', onMapClick);
//通过json添加图层MultiPolygon
var testData={
"type": "FeatureCollection",
"name": "TestData",
"features": [
{"type":"Feature","properties":{"fillOpacity":0.8,"fill":"rgb(163,224,142)","value":"11"},"geometry":{ "type": "Polygon", "coordinates": [[[123,41],[121,39],[126,41]]]},"id":0}
]
};
var testLayer = L.geoJSON(testData, {
style: function (feature) {
return {color: '#4264fb', fillColor:feature.properties.fill, weight:1, fillOpacity:feature.properties.fillOpacity};
}
}).addTo(leafletMap).bindPopup("I am a polygon layer.");
</script>
</body>
</html>
三、总结geometry对象中type属性为"MultiPolygon"时coordinates属性为四维数组。geometry对象中type属性为"Polygon"时coordinates属性为三维数组