简体   繁体   English

从GeoJSON文件中读取纬度和经度,然后使用Leaflet将每个经纬度显示为标记

[英]Read Latitude and Longitude from a GeoJSON file, and then display each lat-long as a marker with Leaflet

I'm new to Javascript and hence I'm a little lost. 我是Java的新手,因此我有点迷路了。 I am able to read from a GeoJSON file; 我可以从GeoJSON文件读取; however, I do not understand how to iterate through the file to get the Lat-Long of the points, and then display those points as markers in Leaflet. 但是,我不明白如何遍历文件以获取点的纬度,然后在传单中将这些点显示为标记。 I am hoping to also use the plugin Awesome Markers (based on font-awesome, for Leaflet) 我希望也使用插件Awesome Markers(基于Leafa的font-awesome)

This is a sample of my GeoJSON file: 这是我的GeoJSON文件的示例:

    { "type": "FeatureCollection",
      "features": [
         { "type": "Feature", "properties": { "Street Nam": "Aljunied Avenue 2", " Block": "118 Aljunied Avenue 2", " Postal Co": "380118", " Latitude": 1.320440, "Longitude": 103.887575 }, 
           "geometry": { "type": "Point", "coordinates": [ 103.887575, 1.320440 ] } }
      ,
      { "type": "Feature", "properties": { "Street Nam": "Aljunied Crescent", " Block": "97A Aljunied Crescent", " Postal Co": "381097", " Latitude": 1.321107, "Longitude": 103.886127 }, 
        "geometry": { "type": "Point", "coordinates": [ 103.886127, 1.321107 ] } }
    ]
    }

Thank you for your attention and time =) 谢谢您的关注和时间=)

Process the geojson as described in the leaflet documentation . 按照传单文档中的描述处理geojson。 Specify a pointToLayer function which creates a marker with an awesome icon: 指定一个pointToLayer函数,该函数将创建带有超赞图标的标记:

L.geoJson(geoJson, {
    pointToLayer: function (feature, latlng) {
        return L.marker(latlng, 
            {icon: L.AwesomeMarkers.icon( 
                 << options based on feature.properties >> 
             )});
    }
}).addTo(map);

After reading the file, you should have a javascript object that represents all the data in the file: 读取文件后,您应该有一个javascript对象,该对象代表文件中的所有数据:

var geoData = JSON.parse(fileContents);
var features = geoData.features;

and so on. 等等。 The parsed data will be converted to objects or arrays depending on whether they are key/value dictionaries or just lists. 解析的数据将转换为对象或数组,具体取决于它们是键/值字典还是列表。 So from the above 所以从上面

var feature = geoData.features[0];

will get you a reference to the first feature object in the list. 将为您提供对列表中第一个要素对象的引用。 If you write 如果你写

console.log(geoData);

and run with any recent browser you should be able to see an expandable description of the data to help make sense of it all. 并在任何最新的浏览器上运行,您都应该能够看到数据的扩展说明,以帮助理解所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM