Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用传单和geoJson监控对象_Javascript_Geojson_Leaflet - Fatal编程技术网

Javascript 使用传单和geoJson监控对象

Javascript 使用传单和geoJson监控对象,javascript,geojson,leaflet,Javascript,Geojson,Leaflet,任务是: 从现有地理数据(一组横向/纵向值)在地图上显示对象(标记)。地理数据定期更新,所以它就像是一种对地图上物体的交互式监控 我正在使用传单框架来实现这个目标。我还使用geoJson输出地理数据(具有lat/long坐标的对象)。以下是我的代码: // for a start make it as a template var geoData = { "type": "FeatureCollection", "features": [] }; // add a feature, a

任务是:

从现有地理数据(一组横向/纵向值)在地图上显示对象(标记)。地理数据定期更新,所以它就像是一种对地图上物体的交互式监控

我正在使用传单框架来实现这个目标。我还使用geoJson输出地理数据(具有lat/long坐标的对象)。以下是我的代码:

// for a start make it as a template
var geoData = {
  "type": "FeatureCollection",
  "features": []
};

// add a feature, assign an id and properties
function addFeature(id, latitude, longitude, properties) {
  // first of all check if the feature have already exists
  var index = IndexOfItem(id);
  if (index >= 0) {
    // if exists then update only coordinates and properties
    geoData.features[index].geometry.coordinates = [longitude, latitude];
    geoData.features[index].properties.popupContent = properties;
  } else {
    // add new feature
    var feature = {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [longitude, latitude]
      },
      "properties": {
        "popupContent": properties
      },

      "id": id
    };
    geoData.features.push(feature);
  }
}

// search and return the index of feature if exists
function IndexOfItem(id) {
  for (var i = 0; i < geoData.features.length; i++) {
    if (id == geoData.features[i].id) {
      return i;
    }
  };
  return -1;
}
//首先,将其作为模板
变量地理数据={
“类型”:“FeatureCollection”,
“功能”:[]
};
//添加要素、指定id和特性
函数addFeature(id、纬度、经度、属性){
//首先检查功能是否已经存在
var指数=指数(id);
如果(索引>=0){
//如果存在,则仅更新坐标和特性
geoData.features[index].geometry.coordinates=[经度,纬度];
geoData.features[index].properties.popupContent=properties;
}否则{
//添加新功能
变量特征={
“类型”:“功能”,
“几何学”:{
“类型”:“点”,
“坐标”:[经度、纬度]
},
“财产”:{
“popupContent”:属性
},
“id”:id
};
地理数据.features.push(特征);
}
}
//搜索并返回特征的索引(如果存在)
函数索引项(id){
对于(var i=0;i
这段代码运行良好。之后,我添加了这些特性的一个新层,当数组更新时(一些特性改变了坐标),我必须从地图中删除该层并创建一个新的L.geoJson(geoData)对象。该过程会一次又一次地重复,同时特征的坐标也会更新

实际上我不擅长JavaScript,只有这样我才能解决这个任务。但在我看来,这就像是一个硬编码,可能有一些JavaScript方法可以更优雅地解决这个问题。有人能给我一个建议(或想法)如何做得更好,甚至获得更好的性能?
我将非常感谢任何帮助

您只需使用L.layerGroup并从该集合中添加和删除即可进行管理。也许还可以使用数据绑定框架来缓存地图视图,以便它可以实时更改,而不必刷新