从 url 加载的多个 GeoJSON 标记未显示
Multiple GeoJSon markers loading from url not showing
我正在尝试制作一个不断更新一群人位置的网站。我正在为此使用 mapbox。
代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title></title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.12.1/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.12.1/mapbox-gl.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<div id='map'></div>
<script>
mapboxgl.accessToken = 'pk.eyJ1Ijoiam9uYXRoYW52b3MiLCJhIjoiY2lpMXJlZmtiMDBlOXRybTBtYmtyNTh0cCJ9.ipvKe_0qEvR18atA-3wggQ';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v8',
center: [5.863, 51.822],
zoom: 15
});
var url = 'http://195.***.**.19/test/tempPaths.json';
var source = new mapboxgl.GeoJSONSource({
data: url
});
window.setInterval(function() {
source.setData(url);
}, 1000);
map.on('style.load', function() {
map.addSource("markers", source);
map.addLayer({
"id": "markers",
"type": "circle",
"source": "markers",
"paint": {
"circle-radius": 10,
"circle-color": "#ff0000"
}
});
});
</script>
</body>
</html>
url 指向包含以下内容的文件:
{
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [5.857907820447556, 51.82389690205782]
},
"properties": {
"title": "Person1"
}
},{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [5.862187668426313, 51.82399055633142]
},
"properties": {
"title": "Person2"
}
},{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [5.866520068371708, 51.82419298727476]
},
"properties": {
"title": "Person3"
}
}]
}
}
文件每秒更新一次。每次更新坐标都会发生变化。
该地图应该显示地图上的每个点,但它是空的。
mapboxgl.GeoJSONSource
需要一个有效的 GeoJSON 对象或 url 其中 returns 一个。您已将 GeoJSON 集合封装在另一个对象中。删除它就可以了:
{
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [5.857907820447556, 51.82389690205782]
},
"properties": {
"title": "Person1"
}
}, {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [5.862187668426313, 51.82399055633142]
},
"properties": {
"title": "Person2"
}
}, {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [5.866520068371708, 51.82419298727476]
},
"properties": {
"title": "Person3"
}
}]
}
我正在尝试制作一个不断更新一群人位置的网站。我正在为此使用 mapbox。
代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title></title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.12.1/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.12.1/mapbox-gl.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<div id='map'></div>
<script>
mapboxgl.accessToken = 'pk.eyJ1Ijoiam9uYXRoYW52b3MiLCJhIjoiY2lpMXJlZmtiMDBlOXRybTBtYmtyNTh0cCJ9.ipvKe_0qEvR18atA-3wggQ';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v8',
center: [5.863, 51.822],
zoom: 15
});
var url = 'http://195.***.**.19/test/tempPaths.json';
var source = new mapboxgl.GeoJSONSource({
data: url
});
window.setInterval(function() {
source.setData(url);
}, 1000);
map.on('style.load', function() {
map.addSource("markers", source);
map.addLayer({
"id": "markers",
"type": "circle",
"source": "markers",
"paint": {
"circle-radius": 10,
"circle-color": "#ff0000"
}
});
});
</script>
</body>
</html>
url 指向包含以下内容的文件:
{
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [5.857907820447556, 51.82389690205782]
},
"properties": {
"title": "Person1"
}
},{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [5.862187668426313, 51.82399055633142]
},
"properties": {
"title": "Person2"
}
},{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [5.866520068371708, 51.82419298727476]
},
"properties": {
"title": "Person3"
}
}]
}
}
文件每秒更新一次。每次更新坐标都会发生变化。 该地图应该显示地图上的每个点,但它是空的。
mapboxgl.GeoJSONSource
需要一个有效的 GeoJSON 对象或 url 其中 returns 一个。您已将 GeoJSON 集合封装在另一个对象中。删除它就可以了:
{
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [5.857907820447556, 51.82389690205782]
},
"properties": {
"title": "Person1"
}
}, {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [5.862187668426313, 51.82399055633142]
},
"properties": {
"title": "Person2"
}
}, {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [5.866520068371708, 51.82419298727476]
},
"properties": {
"title": "Person3"
}
}]
}