从网址加载的多个GeoJSon标记未显示

时间:2015-12-12 00:56:55

标签: javascript json mapbox

我正在努力建立一个不断更新一群人位置的网站。我正在使用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>

网址指向包含以下内容的文件:

{
  "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"
      }
    }]
  }
}

文件每秒更新一次。每次更新时坐标都会改变。 地图应该显示地图上的每个地点,但它是空的。

1 个答案:

答案 0 :(得分:1)

mapboxgl.GeoJSONSource需要一个有效的GeoJSON对象或一个返回一个的url。您已将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"
        }
    }]
}