我正在使用Leaflet和Leaflet-omnivore一起显示gpx数据。我已经在Windows 10上使用Firefox(65.0)和Edge(44.17763.10)浏览器成功测试了代码,但是Chrome(71.0357898)出现了问题。
当我尝试加载“无效” gpx文件(见下文)时,Firefox和Edge均正确报告“就绪异常”(见下文代码)。
但是,对于Chrome,控制台会报告未捕获的错误:-
Uncaught Error: Invalid LatLng object: (48.6, NaN)
但不会触发异常。
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>debug test</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.4.0/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.4.0/dist/leaflet.js"></script>
<script src='https://api.mapbox.com/mapbox.js/plugins/leaflet-omnivore/v0.3.1/leaflet-omnivore.min.js'></script>
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<div id='map'></div>
<script>
const inspect = obj => { // output content of variables/objects to console
for (const prop in obj) {
if (obj.hasOwnProperty(prop)) {
console.log(`${prop}:: ${obj[prop]}`)
}
}
}
var osmMapTiles = L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 18
});
var map = L.map('map');
osmMapTiles.addTo(map);
try {
var runLayer = omnivore.gpx('invalid.gpx')
.on('ready', function() {
try {
map.fitBounds(runLayer.getBounds());
} catch (exception) {
console.log("---- ready exception ----");
inspect(exception);
}
})
.on('error', function(error) {
console.log("---- error exception ----");
inspect(error);
})
.addTo(map);
} catch (exception) {
console.log("---- outer exception ----");
inspect(exception);
}
</script>
</body>
</html>
------------------- invalid.gpx -------------
<?xml version="1.0" encoding="UTF-8"?>
<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/0" version="1.0" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd" creator="gpx.py -- https://github.com/tkrajina/gpxpy">
<trk>
<name>invalid</name>
<trkseg>
<trkpt lat="44.0" lon="1.0"></trkpt>
<trkpt lat="44.0" lon="5.8"></trkpt>
<trkpt lat="48.6" lon="5.8"></trkpt>
<trkpt lat="48.6"
----------------------------------------------
这是Chrome的问题吗?还是我做错了什么?
此后,我已经在Mac上使用Chrome浏览器进行了尝试,并且运行正常。