0

我是 Javascript 和传单的初学者。首先我制作了geojson.js这样的文件

 var data={

"type": "FeatureCollection",
"features": [
    {
        "type": "Feature",
        "id": 1,
        "properties": {
            "Name": "Germany",
            "description": "",
            "timestamp": "",
            "begin": "",
            "end": "",
            "altitudeMode": "clampToGround",
            "tessellate": 1,
            "extrude": -1,
            "visibility": -1
        },
        "geometry": {
            "type": "Point",
            "coordinates": [                     
                        51.329219337279405,
                        10.454119349999928 
            ]
        }
       } 
      ]
     }

并像这样访问他们的坐标

points[0] = data.features[0].geometry.coordinates[0];
points[1] = data.features[0].geometry.coordinates[1];

没关系,但我想制作几个 geojson,将它们存储在同一个文件夹中并通过 url 访问它们leaflet/geojson.js,而不是通过 http 或 ajax。像这样的东西:

 var data = $.getJSON( "leaflet/geojson.js", function(json) {
   points[0] = json.features[0].geometry.coordinates[0];

 })

我能做到吗?怎么做?

提前致谢!

4

1 回答 1

3

如果你想像他们在这里http://leafletjs.com/examples/geojson-example.html那样将geojson存储在一个单独的文件中,那么你将不得不以与浏览器获取js文件相同的方式获取它用于初始化传单。

正如 charlietfl 所说,只需在您的脚本标签上方添加脚本标签,就像他们在我发布的链接中所做的那样。然后,您可以在不使用任何获取方法的情况下引用数据变量,因为它已经在页面加载时获取。或者,只需将 var 数据添加到与您的 init 脚本相同的文件中。

<script type="text/javascript" src="leaflet/geojson.js">
    var data = {

        "type": "FeatureCollection",
        "features": [
            {
                "type": "Feature",
                "id": 1,
                "properties": {
                    "Name": "Germany",
                    "description": "",
                    "timestamp": "",
                    "begin": "",
                    "end": "",
                    "altitudeMode": "clampToGround",
                    "tessellate": 1,
                    "extrude": -1,
                    "visibility": -1
                },
                "geometry": {
                    "type": "Point",
                    "coordinates": [
                        51.329219337279405,
                        10.454119349999928
                    ]
                }
            }
        ]
    };

    var data2 = {
      ...
    };
</script>
<script>
    var map = L.map('map').setView([39.74739, -105], 13);
    L.tileLayer('http://{s}.tile.cloudmade.com/{key}/22677/256/{z}/{x}/{y}.png', {
        attribution: 'Map data &copy; 2011 OpenStreetMap contributors, Imagery &copy; 2012 CloudMade',
        key: 'BC9A493B41014CAABB98F0471D759707'
    }).addTo(map);

    L.geoJson(data).addTo(map);
    L.geoJson(data2).addTo(map);
</script>
于 2013-11-12T01:08:12.097 回答