使用json在传单地图中添加图像

时间:2015-05-29 10:48:25

标签: jquery leaflet geojson

我有一个json文件,每行包含文件名和边界框:

{ "type": "Feature", "properties": { "name": "images\/gis\/json_Andros1.png" }, "geometry": { "type" : "LineString", "coordinates": [[37.8416750531,24.7951207305],[37.8418346487,24.7954102608]] }  }

我通过以下方式访问该文件:

var raster_group = new L.featureGroup([]);
$.getJSON($('link[id="rasters_geojson"]').attr("href"), function(data) { 
    var geojson = L.geoJson(data, {  
        onEachFeature: function (feature, layer) {
            var rasterfilename = (String(feature.properties['name']));
            var rastercoordinates = L.GeoJSON.coordsToLatLngs(feature.geometry['coordinates'],0);
            alert(rasterfilename); alert(rastercoordinates);
            layer = new L.imageOverlay(rasterfilename, rastercoordinates); 
            return layer;
        }
    });
    geojson.addTo(raster_group);
});

错误是NS_ERROR_NOT_AVAILABLE:

另一个问题。如何只加载地图边界框内的图像?

2 个答案:

答案 0 :(得分:0)

你的路线 var rastercoordinates = coordsToLatLngs(feature.properties['geometry'],0); 缺少方法的类。 应该是这样的 var rastercoordinates = L.GeoJSON.coordsToLatLngs(feature.properties['geometry'],0);

答案 1 :(得分:0)

我非常感谢snkashis的帮助。事实证明事情更加复杂。

首先,我遇到了传单反转从json读取的坐标的问题。然后我遇到了不同类型对象的问题,这些问题导致了传单本身的错误(递归过多,未定义的变量等)。

但是下面的代码有效,所以我将其上传给其他人使用:

mapinitialbounds = map.getBounds();
var raster_group = new L.featureGroup([]);
    $.getJSON($('link[id="rasters_geojson"]').attr("href"), function(data) { 
        var geojson = L.geoJson(data, {  
            onEachFeature: function (feature, layer) {
                var rasterfilename = (String(feature.properties['name']));
                var rastercoordinates = [feature.geometry['coordinates'][0],feature.geometry['coordinates'][1]];
                var line = new L.polyline(rastercoordinates);
                if (mapinitialbounds.contains(line.getBounds())) { 
                    layer = new L.imageOverlay(rasterfilename, rastercoordinates); 
                    layer.addTo(raster_group);
                }
                return layer;
            }
        });
    });
    map.addLayer(raster_group);

我发现需要创建一条折线来获得不必要的边界。你认为上面的代码可以简化吗?

相关问题