从python传递json数据(瓶子.py)要显示在滑动的地图上

2024-10-02 10:23:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个我认为很简单的副业项目。事实证明,由于我对python一无所知(我使用的是PHP),所以我有点不太舒服。我正在尝试创建一个滑动地图(使用cloudemade的传单),显示一组兴趣点。我有一个CSV文件,包含poi的名称、纬度和经度。在

到目前为止,我能够读取CSV数据并将其转换为JSON数据,并在web页面中显示JSON数据的转储瓶子.py. 在

我的问题是我怎么通过瓶子.py的JSON数据通过jquery传输到spreal的javascript库??在

克里斯


Tags: 文件csv数据项目py名称json瓶子
1条回答
网友
1楼 · 发布于 2024-10-02 10:23:24

spreal支持GEOJSON,这使得它可以很容易地用spreal显示向量数据。在

参见:

http://leaflet.cloudmade.com/examples/geojson.html

python geojson库也支持geojson,因此在python端,除了将其放入适当的对象中并在将生成的JSON提供给sloper之前添加所需的任何附加属性外,您不需要做太多工作。在

只需使用一个标准的XMLHttpRequest()从您提供服务的位置获取JSON对象。在

function loadData(){

    // retrieve and load features to a layer
    var xhrequest = new XMLHttpRequest();
    xhrequest.onreadystatechange = function(){
        if (xhrequest.readyState == 4){
            if (xhrequest.status == 200){
                var geojsonLayer = new L.GeoJSON();
                geojsonLayer.on("featureparse", function(e){
                    if (e.properties && e.properties.name && e.properties.value){
                        var popupContent = "Whatever added properties you want to display";
                        e.layer.bindPopup(popupContent);                     
                    }; 
                    // any styles you've added
                    if (e.properties && e.properties.style && e.layer.setStyle){
                        e.layer.setStyle(e.properties.style);                      
                    };                      
                });    
                var layerItems =  JSON.parse(xhrequest.responseText);
                // add features to layer
                for (i=0; i<layerItems.length;i++){
                    var geojsonFeature = layerItems[i];
                    geojsonLayer.addGeoJSON(geojsonFeature);                       
                };
                map.addLayer(geojsonLayer); 
            };                
        };            
    };
    var url= "<url of where you are serving the GEOJSON data>"; 
    xhrequest.open('GET', url, true);
    xhrequest.send(null);            
}

相关问题 更多 >

    热门问题