Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function ($) {
- Drupal.behaviors.leaflet = function (context) {
- if (context == document) {
- // Fix breadcrumb settings
- $('#leaflet-map-form .form-item:not(#edit-continents-wrapper)').hide();
- $('#edit-continents').change(function() {
- $('#leaflet-map-form .form-item:not(#edit-continents-wrapper)').hide();
- $('#edit-continent-' + $(this).val() + '-wrapper').show();
- });
- $('#leaflet-map-form select:not(#edit-continents)').change(function() {
- // iso2 $(this).val();
- });
- // initialize the map on the "map" div with a given center and zoom
- var map = new L.Map('map', {
- center: new L.LatLng(51.505, -0.09),
- zoom: 2,
- minZoom: 2,
- });
- var bounds = new L.Bounds();
- // Initialize hover country
- var countries = {};
- // Initialize hover country
- var continent = new L.GeoJSON(null);
- var countriesArr = {};
- // Hover country style
- var country_style = {
- color: "#3d8a1e",
- weight: 1,
- opacity: 1,
- fillOpacity: 1,
- fillColor:"#C5FF94",
- clickable:false
- }
- // Hover country style
- var continent_style = {
- color: "#3d8a1e",
- weight: 1,
- opacity: 1,
- fillOpacity: 1,
- fillColor:"#C5FF94",
- clickable:false
- }
- map.on('zoomend', function(event){
- if(map.getZoom()>=3) {
- map.removeLayer(continents);
- map.addLayer(countries);
- //$('#leaflet-map-form select:not(#edit-continents)').val('empty');
- // $('#leaflet-map-form .form-item:not(#edit-continents-wrapper)').hide();
- }
- if(map.getZoom()<3) {
- map.removeLayer(countries);
- map.addLayer(continents);
- // Hide and remove in any case the hover country
- country.cartodb_id = null;
- country.off("featureparse");
- map.removeLayer(country)
- }
- });
- // Create the CartoDB layer
- var countries = new L.CartoDBLayer({
- map: map,
- user_name:"goalgorilla",
- table_name: 'countries',
- query: "SELECT iso2, continent, cartodb_id, ST_SIMPLIFY(the_geom_webmercator,0.02) as the_geom_webmercator, " +
- "ST_ASGEOJSON(ST_SIMPLIFY(the_geom,0.02)) as geometry FROM {{table_name}}",
- opacity: 1,
- interactivity: "cartodb_id, geometry, iso2, continent",
- featureOver: function(ev,latlng, pos, data) {
- document.body.style.cursor = "pointer";
- if (!countries[data.iso2]) {
- countries[data.iso2] = new L.GeoJSON(null);
- }
- // Show the hover country if it is a different feature
- if (data.cartodb_id != countries[data.iso2].cartodb_id) {
- if (countries[data.iso2]) {
- map.removeLayer(countries[data.iso2]);
- countries[data.iso2].off("featureparse");
- }
- else {
- countries[data.iso2] = new L.GeoJSON(null);
- }
- countries[data.iso2].cartodb_id = data.cartodb_id;
- // Change style
- countries[data.iso2].on("featureparse", function (e) {
- e.layer.setStyle(country_style);
- });
- countries[data.iso2].addGeoJSON(JSON.parse(data.geometry));
- map.addLayer(countries[data.iso2]);
- }
- },
- featureOut: function() {
- document.body.style.cursor = "default";
- // Hide and remove in any case the hover country
- countries[data.iso2].cartodb_id = null;
- countries[data.iso2].off("featureparse");
- map.removeLayer(countries[data.iso2]);
- },
- featureClick: function(ev,latlng,pos,data) {
- $('#leaflet-map-form .form-item:not(#edit-continents-wrapper)').hide();
- $('#edit-continent-' + data.continent + '-wrapper').show();
- $('#edit-continents').val(data.continent);
- $('#edit-continent-' + data.continent).val(data.iso2);
- bounds = countries[data.iso2].getBounds();
- map.fitBounds(bounds);
- },
- auto_bound: true,
- });
- // Create the CartoDB layer
- var continents = new L.CartoDBLayer({
- map: map,
- user_name:"goalgorilla",
- table_name: 'continents',
- query: "SELECT cartodb_id, iso, zoomlon, zoomlat, zoom, ST_SIMPLIFY(the_geom_webmercator,0.1) as the_geom_webmercator, " +
- "ST_ASGEOJSON(ST_SIMPLIFY(the_geom,0.1)) as geometry FROM {{table_name}}",
- opacity: 1,
- interactivity: "cartodb_id, geometry, iso, zoomlon, zoomlat, zoom",
- featureOver: function(ev,latlng, pos, data) {
- document.body.style.cursor = "pointer";
- // Show the hover continent if it is a different feature
- if (data.cartodb_id != continent.cartodb_id) {
- map.removeLayer(continent);
- continent.off("featureparse");
- continent = new L.GeoJSON(null);
- continent.cartodb_id = data.cartodb_id;
- // Change style
- continent.on("featureparse", function (e) {
- e.layer.setStyle(continent_style);
- });
- continent.addGeoJSON(JSON.parse(data.geometry));
- map.addLayer(continent);
- }
- },
- featureOut: function() {
- document.body.style.cursor = "default";
- // Hide and remove in any case the hover continent
- continent.cartodb_id = null;
- continent.off("featureparse");
- map.removeLayer(continent)
- },
- featureClick: function(ev,latlng,pos,data) {
- document.body.style.cursor = "default";
- $('#leaflet-map-form .form-item:not(#edit-continents-wrapper)').hide();
- $('#edit-continent-' + data.iso + '-wrapper').show();
- $('#edit-continents').val(data.iso);
- $('#edit-continent-' + data.iso).val('empty');
- // Hide and remove in any case the hover continent
- map.removeLayer(continent);
- latlng = new L.LatLng(data.zoomlat, data.zoomlon);
- map.setView( latlng, data.zoom, false);
- },
- auto_bound: true,
- });
- // Adding the layer to map
- map.addLayer(continents);
- // Initial zoom
- latlng = new L.LatLng(24.5271348225978, 23.5546875);
- map.setView( latlng, 2, false);
- }
- }
- })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement