Advertisement
Guest User

Untitled

a guest
Jul 11th, 2012
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.47 KB | None | 0 0
  1. (function ($) {
  2.  
  3. Drupal.behaviors.leaflet = function (context) {
  4. if (context == document) {
  5.  
  6. // Fix breadcrumb settings
  7. $('#leaflet-map-form .form-item:not(#edit-continents-wrapper)').hide();
  8.  
  9. $('#edit-continents').change(function() {
  10. $('#leaflet-map-form .form-item:not(#edit-continents-wrapper)').hide();
  11. $('#edit-continent-' + $(this).val() + '-wrapper').show();
  12. });
  13.  
  14. $('#leaflet-map-form select:not(#edit-continents)').change(function() {
  15. // iso2 $(this).val();
  16. });
  17.  
  18. // initialize the map on the "map" div with a given center and zoom
  19. var map = new L.Map('map', {
  20. center: new L.LatLng(51.505, -0.09),
  21. zoom: 2,
  22. minZoom: 2,
  23. });
  24.  
  25. var bounds = new L.Bounds();
  26.  
  27. // Initialize hover country
  28. var countries = {};
  29.  
  30. // Initialize hover country
  31. var continent = new L.GeoJSON(null);
  32.  
  33. var countriesArr = {};
  34.  
  35. // Hover country style
  36. var country_style = {
  37. color: "#3d8a1e",
  38. weight: 1,
  39. opacity: 1,
  40. fillOpacity: 1,
  41. fillColor:"#C5FF94",
  42. clickable:false
  43. }
  44.  
  45. // Hover country style
  46. var continent_style = {
  47. color: "#3d8a1e",
  48. weight: 1,
  49. opacity: 1,
  50. fillOpacity: 1,
  51. fillColor:"#C5FF94",
  52. clickable:false
  53. }
  54.  
  55. map.on('zoomend', function(event){
  56. if(map.getZoom()>=3) {
  57. map.removeLayer(continents);
  58. map.addLayer(countries);
  59. //$('#leaflet-map-form select:not(#edit-continents)').val('empty');
  60. // $('#leaflet-map-form .form-item:not(#edit-continents-wrapper)').hide();
  61. }
  62.  
  63. if(map.getZoom()<3) {
  64. map.removeLayer(countries);
  65. map.addLayer(continents);
  66.  
  67. // Hide and remove in any case the hover country
  68. country.cartodb_id = null;
  69. country.off("featureparse");
  70. map.removeLayer(country)
  71. }
  72. });
  73.  
  74. // Create the CartoDB layer
  75. var countries = new L.CartoDBLayer({
  76. map: map,
  77. user_name:"goalgorilla",
  78. table_name: 'countries',
  79. query: "SELECT iso2, continent, cartodb_id, ST_SIMPLIFY(the_geom_webmercator,0.02) as the_geom_webmercator, " +
  80. "ST_ASGEOJSON(ST_SIMPLIFY(the_geom,0.02)) as geometry FROM {{table_name}}",
  81. opacity: 1,
  82. interactivity: "cartodb_id, geometry, iso2, continent",
  83. featureOver: function(ev,latlng, pos, data) {
  84. document.body.style.cursor = "pointer";
  85.  
  86. if (!countries[data.iso2]) {
  87. countries[data.iso2] = new L.GeoJSON(null);
  88. }
  89.  
  90. // Show the hover country if it is a different feature
  91. if (data.cartodb_id != countries[data.iso2].cartodb_id) {
  92. if (countries[data.iso2]) {
  93. map.removeLayer(countries[data.iso2]);
  94. countries[data.iso2].off("featureparse");
  95. }
  96. else {
  97. countries[data.iso2] = new L.GeoJSON(null);
  98. }
  99. countries[data.iso2].cartodb_id = data.cartodb_id;
  100.  
  101. // Change style
  102. countries[data.iso2].on("featureparse", function (e) {
  103. e.layer.setStyle(country_style);
  104. });
  105.  
  106. countries[data.iso2].addGeoJSON(JSON.parse(data.geometry));
  107. map.addLayer(countries[data.iso2]);
  108. }
  109. },
  110. featureOut: function() {
  111. document.body.style.cursor = "default";
  112.  
  113. // Hide and remove in any case the hover country
  114. countries[data.iso2].cartodb_id = null;
  115. countries[data.iso2].off("featureparse");
  116. map.removeLayer(countries[data.iso2]);
  117. },
  118. featureClick: function(ev,latlng,pos,data) {
  119. $('#leaflet-map-form .form-item:not(#edit-continents-wrapper)').hide();
  120. $('#edit-continent-' + data.continent + '-wrapper').show();
  121.  
  122. $('#edit-continents').val(data.continent);
  123. $('#edit-continent-' + data.continent).val(data.iso2);
  124.  
  125. bounds = countries[data.iso2].getBounds();
  126. map.fitBounds(bounds);
  127. },
  128. auto_bound: true,
  129. });
  130.  
  131.  
  132. // Create the CartoDB layer
  133. var continents = new L.CartoDBLayer({
  134. map: map,
  135. user_name:"goalgorilla",
  136. table_name: 'continents',
  137. query: "SELECT cartodb_id, iso, zoomlon, zoomlat, zoom, ST_SIMPLIFY(the_geom_webmercator,0.1) as the_geom_webmercator, " +
  138. "ST_ASGEOJSON(ST_SIMPLIFY(the_geom,0.1)) as geometry FROM {{table_name}}",
  139. opacity: 1,
  140. interactivity: "cartodb_id, geometry, iso, zoomlon, zoomlat, zoom",
  141. featureOver: function(ev,latlng, pos, data) {
  142. document.body.style.cursor = "pointer";
  143.  
  144. // Show the hover continent if it is a different feature
  145. if (data.cartodb_id != continent.cartodb_id) {
  146. map.removeLayer(continent);
  147. continent.off("featureparse");
  148.  
  149. continent = new L.GeoJSON(null);
  150. continent.cartodb_id = data.cartodb_id;
  151.  
  152. // Change style
  153. continent.on("featureparse", function (e) {
  154. e.layer.setStyle(continent_style);
  155. });
  156.  
  157. continent.addGeoJSON(JSON.parse(data.geometry));
  158. map.addLayer(continent);
  159. }
  160. },
  161. featureOut: function() {
  162. document.body.style.cursor = "default";
  163.  
  164. // Hide and remove in any case the hover continent
  165. continent.cartodb_id = null;
  166. continent.off("featureparse");
  167. map.removeLayer(continent)
  168. },
  169. featureClick: function(ev,latlng,pos,data) {
  170. document.body.style.cursor = "default";
  171.  
  172. $('#leaflet-map-form .form-item:not(#edit-continents-wrapper)').hide();
  173. $('#edit-continent-' + data.iso + '-wrapper').show();
  174. $('#edit-continents').val(data.iso);
  175. $('#edit-continent-' + data.iso).val('empty');
  176.  
  177. // Hide and remove in any case the hover continent
  178. map.removeLayer(continent);
  179. latlng = new L.LatLng(data.zoomlat, data.zoomlon);
  180. map.setView( latlng, data.zoom, false);
  181. },
  182. auto_bound: true,
  183. });
  184.  
  185. // Adding the layer to map
  186. map.addLayer(continents);
  187.  
  188. // Initial zoom
  189. latlng = new L.LatLng(24.5271348225978, 23.5546875);
  190. map.setView( latlng, 2, false);
  191. }
  192. }
  193.  
  194. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement