Guest User

Untitled

a guest
Feb 23rd, 2018
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Example</title>
  5. <link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.3/dist/leaflet.css" />
  6. <script src="https://unpkg.com/leaflet@1.0.3/dist/leaflet.js"></script>
  7. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
  8. <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  9. <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  10.  
  11. <script src="https://npmcdn.com/@turf/turf/turf.min.js"></script>
  12.  
  13. <style>
  14.  
  15. #map {
  16. width: 800px;
  17. height: 600px;
  18. border: 1px solid black;
  19. z-index: 0;
  20. }
  21. </style>
  22. </head>
  23.  
  24. <body>
  25.  
  26. <div id="map" ></div>
  27. </div>
  28.  
  29. <script>
  30. var url = 'usa.json'; // my GeoJSON data source, in this case a static file not a live PHP data feed.
  31. //'http://www.gistechsolutions.com/leaflet/DEMO/Search/usa.json'
  32.  
  33. var map = L.map('map').setView([47.7541, -107.05078], 3);
  34.  
  35. var osm=new L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png',{
  36. attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'});
  37.  
  38. var OpenStreetMap_BlackAndWhite = L.tileLayer('http://{s}.tiles.wmflabs.org/bw-mapnik/{z}/{x}/{y}.png', {
  39. maxZoom: 18,
  40. attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
  41. });
  42. OpenStreetMap_BlackAndWhite.addTo(map);
  43.  
  44.  
  45. // Set style function that sets fill color property
  46. function style(feature) {
  47. return {
  48. fillColor: 'green',
  49. fillOpacity: 0.5,
  50. weight: 2,
  51. opacity: 1,
  52. color: '#ffffff',
  53. dashArray: '3'
  54. };
  55. }
  56.  
  57. // Null variable that will hold layer
  58. var stateLayer = L.geoJson(null, { style: style});
  59.  
  60. $.getJSON(url, function(data) {
  61. stateLayer.addData(data);
  62. });
  63.  
  64. stateLayer.addTo(map);
  65.  
  66. map.on('click',function(e){
  67. lat = e.latlng.lat;
  68. lon = e.latlng.lng;
  69. ProcessClick(lat,lon)
  70. });
  71.  
  72. var theMarker;
  73. var selPoly = [];
  74.  
  75. function ProcessClick(lat,lon){
  76. if (theMarker != undefined) {
  77. map.removeLayer(theMarker);
  78. };
  79. theMarker = L.marker([lat,lon]).addTo(map);
  80.  
  81. stateLayer.eachLayer(function (layer) {
  82.  
  83. isInside =turf.inside(theMarker.toGeoJSON(), layer.toGeoJSON());
  84.  
  85. if (isInside){
  86. selPoly.push(layer.feature);
  87. console.log(layer.feature.properties.STATE_NAME);
  88. }
  89.  
  90. })
  91.  
  92. var newgeojsonLayer = L.geoJson(selPoly, {
  93. color: 'orange',
  94. fillOpacity: .5,
  95. opacity: 1
  96. }).addTo(map);
  97. }
  98.  
  99. var baseMaps = {
  100. "Open Street Map": osm,
  101. "OSM B&W":OpenStreetMap_BlackAndWhite
  102. };
  103.  
  104. var overlayMaps = {
  105. "USA":stateLayer
  106. };
  107. //Add layer control
  108. L.control.layers(baseMaps, overlayMaps).addTo(map);
  109.  
  110. </script>
  111. </body>
  112. </html>
Add Comment
Please, Sign In to add comment