关键词搜索

源码搜索 ×
×

Leaflet文档阅读笔记-Using GeoJSON with Leaflet笔记

发布2019-09-09浏览4897次

详情内容

目录

 

 

官方解析

博主例子


 

官方解析

个人觉得这官方教程给得是相当的好,因为功能非常的强大,在此记录下,方便以后使用,以后肯定会经常用到!

此节将会学到从GeoJson对象中创建且调用map vectors。

GeoJson对象如下:

  1. var geojsonFeature = {
  2. "type": "Feature",
  3. "properties": {
  4. "name": "Coors Field",
  5. "amenity": "Baseball Stadium",
  6. "popupContent": "This is where the Rockies play!"
  7. },
  8. "geometry": {
  9. "type": "Point",
  10. "coordinates": [-104.99404, 39.75621]
  11. }
  12. };

可以通过下面这两种方式在地图上添加geojson

L.geoJSON(geojsonFeature).addTo(map);

或者

  1. var myLayer = L.geoJSON().addTo(map);
  2. myLayer.addData(geojsonFeature);

两种方式设置其风格

  1. var myLines = [{
  2. "type": "LineString",
  3. "coordinates": [[-100, 40], [-105, 45], [-110, 55]]
  4. }, {
  5. "type": "LineString",
  6. "coordinates": [[-105, 40], [-110, 45], [-115, 55]]
  7. }];
  8. var myStyle = {
  9. "color": "#ff7800",
  10. "weight": 5,
  11. "opacity": 0.65
  12. };
  13. L.geoJSON(myLines, {
  14. style: myStyle
  15. }).addTo(map);

  1. var states = [{
  2. "type": "Feature",
  3. "properties": {"party": "Republican"},
  4. "geometry": {
  5. "type": "Polygon",
  6. "coordinates": [[
  7. [-104.05, 48.99],
  8. [-97.22, 48.98],
  9. [-96.58, 45.94],
  10. [-104.03, 45.94],
  11. [-104.05, 48.99]
  12. ]]
  13. }
  14. }, {
  15. "type": "Feature",
  16. "properties": {"party": "Democrat"},
  17. "geometry": {
  18. "type": "Polygon",
  19. "coordinates": [[
  20. [-109.05, 41.00],
  21. [-102.06, 40.99],
  22. [-102.03, 36.99],
  23. [-109.04, 36.99],
  24. [-109.05, 41.00]
  25. ]]
  26. }
  27. }];
  28. L.geoJSON(states, {
  29. style: function(feature) {
  30. switch (feature.properties.party) {
  31. case 'Republican': return {color: "#ff0000"};
  32. case 'Democrat': return {color: "#0000ff"};
  33. }
  34. }
  35. }).addTo(map);

在地图上画一个点
 

  1. var geojsonMarkerOptions = {
  2. radius: 8,
  3. fillColor: "#ff7800",
  4. color: "#000",
  5. weight: 1,
  6. opacity: 1,
  7. fillOpacity: 0.8
  8. };
  9. L.geoJSON(someGeojsonFeature, {
  10. pointToLayer: function (feature, latlng) {
  11. return L.circleMarker(latlng, geojsonMarkerOptions);
  12. }
  13. }).addTo(map);

当选点击点后,弹出某窗口

  1. function onEachFeature(feature, layer) {
  2. // does this feature have a property named popupContent?
  3. if (feature.properties && feature.properties.popupContent) {
  4. layer.bindPopup(feature.properties.popupContent);
  5. }
  6. }
  7. var geojsonFeature = {
  8. "type": "Feature",
  9. "properties": {
  10. "name": "Coors Field",
  11. "amenity": "Baseball Stadium",
  12. "popupContent": "This is where the Rockies play!"
  13. },
  14. "geometry": {
  15. "type": "Point",
  16. "coordinates": [-104.99404, 39.75621]
  17. }
  18. };
  19. L.geoJSON(geojsonFeature, {
  20. onEachFeature: onEachFeature
  21. }).addTo(map);

过滤

map中不显示"Busch Field"

  1. var someFeatures = [{
  2. "type": "Feature",
  3. "properties": {
  4. "name": "Coors Field",
  5. "show_on_map": true
  6. },
  7. "geometry": {
  8. "type": "Point",
  9. "coordinates": [-104.99404, 39.75621]
  10. }
  11. }, {
  12. "type": "Feature",
  13. "properties": {
  14. "name": "Busch Field",
  15. "show_on_map": false
  16. },
  17. "geometry": {
  18. "type": "Point",
  19. "coordinates": [-104.98404, 39.74621]
  20. }
  21. }];
  22. L.geoJSON(someFeatures, {
  23. filter: function(feature, layer) {
  24. return feature.properties.show_on_map;
  25. }
  26. }).addTo(map);

 

博主例子

这里举个例子,GIS为自己本地搭建的。

把南京和合肥圈了出来,如下图所示:

当点击某个圆后

代码如下:

test5.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Hello Test5</title>
  5. <meta charset="utf-8" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <link rel="shortcut icon" type="image/x-icon" href="docs/images/favicon.ico" />
  8. <link rel="stylesheet" href="leaflet.css" />
  9. <script src="leaflet.js"></script>
  10. <script src="leaflet-tilelayer-wmts-src.js"></script>
  11. <script src="echarts.js"></script>
  12. <style>
  13. html, body {
  14. height: 100%;
  15. margin: 0;
  16. }
  17. #map {
  18. width: 100%;
  19. height: 100%;
  20. }
  21. .chart{
  22. width: 600px;
  23. height: 300px;
  24. background-color: #fff;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <script src="geojson.js" type="text/javascript"></script>
  30. <div id='map'></div>
  31. <script type="text/javascript">
  32. var ign = new L.TileLayer.WMTS( "http://XXX.XXX.XXX.XXX:8080/geoserver/gwc/service/wmts" ,
  33. {
  34. layer: 'GG_9:gg_9',
  35. tilematrixset: "EPSG:900913",
  36. Format : 'image/png',
  37. TileMatrix: 'EPSG:900913:8'
  38. }
  39. );
  40. var map = L.map('map', {
  41. minZoom: 5,
  42. maxZoom: 7
  43. }).setView([32, 118], 7);
  44. L.control.scale({'position':'bottomleft','metric':true,'imperial':false}).addTo(map);
  45. map.addLayer(ign);
  46. map.invalidateSize(true);
  47. function onEachFeature(feature, layer) {
  48. var popupContent = "弹出窗口,此城市为:" + feature.geometry.properties.popupContent;
  49. layer.bindPopup(popupContent);
  50. }
  51. //新加的代码
  52. L.geoJSON([bicycleRental], {
  53. onEachFeature: onEachFeature,
  54. pointToLayer: function (feature, latlng) {
  55. return L.circleMarker(latlng, {
  56. radius: 8,
  57. fillColor: "#ff7800",
  58. color: "#000",
  59. weight: 1,
  60. opacity: 1,
  61. fillOpacity: 0.8
  62. });
  63. }
  64. }).addTo(map);
  65. //新加的代码
  66. </script>
  67. </body>
  68. </html>

geojson.js

  1. var bicycleRental = {
  2. "type" : "FeatureConllection",
  3. "features" : [
  4. {
  5. "geometry" : {
  6. "type" : "Point",
  7. "coordinates" : [118.8, 32.05],
  8. "properties": {
  9. "popupContent" : "南京"
  10. }
  11. },
  12. "type" : "Feature",
  13. "id" : 100
  14. },
  15. {
  16. "geometry" : {
  17. "type" : "Point",
  18. "coordinates" : [117.242, 31.8],
  19. "properties": {
  20. "popupContent" : "合肥"
  21. }
  22. },
  23. "type" : "Feature",
  24. "id" : 101
  25. }
  26. ]
  27. };

这里要注意一点!

此处的经纬度是反过来的!!!!!

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载