首页 文章

从Leaflet导出GPX文件

提问于
浏览
0

我想要做的是让用户通过选择Leaflet中的一些GeoJson功能来创建GPX文件 . 我这样做的方法是创建一个新的GeoJson图层来存储所选的特征,然后用一个名为togpx(https://github.com/tyrasd/togpx)的插件将其转换为gpx . 现在我有一个gpx文件,但我没有't know how can I let the users download it. Any suggestions? Here'我的代码:

var GPXfile;
var trails = new L.GeoJSON.AJAX('data/trasee.geojson', {
    onEachFeature: function(feature, layer) {
        layer.on({
            click: function () {
                var selectedGeojson = {
                  "type": "FeatureCollection",
                  "features": [
                    {
                      "type": "Feature",
                      "properties": {
                       "name": "Rocka Rolla"
                      },
                      "geometry": {
                        "type": "LineString",
                        "coordinates": feature.geometry.coordinates
                      }
                    }]
                }
                GPXfile = togpx(selectedGeojson);
            }
        })
    }
}).addTo(map);

一个JsFiddle可能会有所帮助:http://jsfiddle.net/pufanalexandru/20ara4qe/1/

1 回答

  • 2

    你可以试试......

    触发下载的链接:

    <a href="#" download="MyTracks.gpx" id="exportGPX">Export to file</a>
    

    一些javascript(你必须包括jquery):

    $("#exportGPX").on('click', function (event) {
    
            // prepare the string that is going to go into the href attribute
            // data:mimetype;encoding,string
            data = 'data:application/javascript;charset=utf-8,' + encodeURIComponent(gpxData);
    
            // set attributes href and target in the <a> element (with id  exportGPX)
            $(this).attr({
                'href': data,
                'target': '_blank'
            });
    
            // let the click go through
        });
    

    这里有一个例子:http://jsfiddle.net/FranceImage/vxe23py4/

    注意:它适用于Chrome,但您也应该尝试使用其他浏览器 .

相关问题