Replace list view google map code with leaflet
authorLaury GvR <laury@gaslightmedia.com>
Wed, 22 Aug 2018 20:30:01 +0000 (16:30 -0400)
committerLaury GvR <laury@gaslightmedia.com>
Wed, 22 Aug 2018 20:30:01 +0000 (16:30 -0400)
glm-member-db/views/front/members/list.html

index 83ba5e4..db09089 100644 (file)
@@ -2,6 +2,24 @@
     {if $settings.list_map_show_opened}map-opened{else}map-closed{/if}
     {if $settings.list_show_search_filters_opened}filters-opened{else}filters-closed{/if}
 ">
+
+            {if $settings.selected_map_interface == 1} 
+                <!--  Leaflet Map -->
+                <link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.3/dist/leaflet.css" integrity="sha512-Rksm5RenBEKSKFjgI3a41vrjkw4EVPlJ3+OiI65vTjIdo9brlAacEuKOiQ5OFh7cOI1bkDwLqdLw3Zg0cRJAAQ==" crossorigin=""/>
+                <script src="https://unpkg.com/leaflet@1.3.3/dist/leaflet.js" integrity="sha512-tAGcCfR4Sc5ZP5ZoVz0quoZDYX5aCtEm/eu1KhSLj2c9eFrylXZknQYmxUssFaVJKvvc0dJQixhGjG2yXWiV9Q==" crossorigin=""></script>
+                <link rel="stylesheet" href="{$jsUrl}/LeafletMarkerCluster/dist/MarkerCluster.Default.css" />
+                <script src="{$jsUrl}/LeafletMarkerCluster/dist/leaflet.markercluster-src.js"></script>
+                <link rel="stylesheet" href="{$jsUrl}/Leaflet.loading/src/Control.Loading.css" />
+                <script src="{$jsUrl}/Leaflet.loading/src/Control.Loading.js"></script>
+            {/if}
+            
+            {if $settings.selected_map_interface == 2}            
+                <!-- Google Map -->
+                <script src="//maps.googleapis.com/maps/api/js?&key={$settings.google_maps_api_key}"></script>
+            {/if}
+
+
+
 {include file='front/members/header.html'}
         {apply_filters('glm-member-db-front-members-list-pageTop', '')}
 <div class="glm-member-list-inner-wrapper">
         </div> <!-- glm-member-list-wrapper -->
         {/if} {*list_show_search*}
     {if $settings.list_show_map}
-        <script src="//maps.googleapis.com/maps/api/js?sensor=true&key={$settings.google_maps_api_key}"></script>
+
         {apply_filters('glm-member-db-front-members-list-mapTop', '')}
         <div id="glm-locationMap-container">
-            <div id="glm-locationMap" class="glm-map">(map loads here)</div>
+
+            {if $settings.selected_map_interface == 1} 
+                <!--  Leaflet Map -->
+                <div id="LeafletMapContainer" style="height: 400px; width: 100%; border: 1px black solid; z-index: +0;"></div>
+            {/if}
+            
+            {if $settings.selected_map_interface == 2}            
+                <!-- Google Map -->
+                <div id="glm-locationMap" class="glm-map">(map loads here)</div>
+            {/if}
+
         </div>
         {apply_filters('glm-member-db-front-members-list-mapBottom', '')}
     {/if}
                     });
             {/if} // settings.list_show_search
 
-            {if $settings.list_show_map}
-                        /*
-                         * Google Maps
-                         *  API reference: https://developers.google.com/maps/documentation/javascript/reference
-                         */
-
-
-                        // Create a Google Map object
-                        var map = new google.maps.Map(document.getElementById('glm-locationMap'), {
-                                center: new google.maps.LatLng({$settings.maps_default_lat}, {$settings.maps_default_lon}),
-                                zoom: {$settings.maps_default_zoom},
-                            disableDefaultUI: false,
-                            mapTypeId: google.maps.MapTypeId.MAP,
-                        });
-
-                        $("#glm-member-list-map-toggle").click( function() {
-                            $(".glm-member-db-{$view}-view").toggleClass("map-opened");
-                            $(".glm-member-db-{$view}-view").toggleClass("map-closed");
-                            var center = map.getCenter();
-                            google.maps.event.trigger(map, "resize");
-                            map.setCenter(center);
-
-                            var currentScroll = $(window).scrollTop(); // get current position
-                            var mapTop = $("#glm-locationMap-container").offset().top-10;
-                            {if $settings.list_show_search}fixmeTop = $('#glm-member-list-filters-button').offset().top;{/if}
-                            if (currentScroll >= mapTop) {
-                                $('body,html').animate({
-                                scrollTop: $("#glm-locationMap-container").offset().top-100
-                                }, 50);
-                            } else {
-                                $('body,html').animate({
-                                    scrollTop: $("#glm-locationMap-container").offset().top-100
-                                }, 400);
-                            }
-                        });
-                        var geocoder = new google.maps.Geocoder();
-                        var bounds = new google.maps.LatLngBounds();
-                        var infowindow = new google.maps.InfoWindow();
-
-                        // Don't Try HTML5 to get user geolocation
-                        //if(navigator.geolocation) {
-                            //navigator.geolocation.getCurrentPosition(function(position) {
-                                //var myLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
-                                //var pinIcon = new google.maps.MarkerImage(
-                                        //'https://maps.google.com/mapfiles/kml/shapes/man.png',
-                                    //null, /* size is determined at runtime */
-                                    //null, /* origin is 0,0 */
-                                    //null, /* anchor is bottom center of the scaled image */
-                                    //new google.maps.Size(30, 30)
-                                //);
-                                //var myMarker = new google.maps.Marker({
-                                    //map: map,
-                                    //position: myLocation,
-                                    //draggable: false,
-                                    //animation: google.maps.Animation.DROP,
-                                    //title: 'My Location',
-                                    //icon: pinIcon,
-                                    //zIndex: 100
-                                //});
-                            //});
-                        //}
-
-
-                {if $mapItems}
-                    var markers = [];
-                    {foreach $mapItems as $m}
-                      {if $m.lat != 0 && $m.lon != 0}
-
-                        // Create a marker for this member
-                        var marker = new google.maps.Marker({
-                            map: map,
-                            position: new google.maps.LatLng({$m.lat}, {$m.lon}),
-                            draggable: false,
-                            animation: google.maps.Animation.DROP,
-                            title: '{$m.member_name|escape:quotes}',
-                            descr: $('#map_info_' + {$m.id}).html(),
-                            memberID: {$m.member}
-                        });
-
-                        // Add a click listener for this marker
-                        marker.addListener('click', function() {
-                                infowindow.setOptions({
-                                        content: this.descr
-                                });
-                            infowindow.open(map,this);
-                        });
-
-                        markers.push(marker);
-
-                        // Extend the map bounds to include this marker
-                        bounds.extend(marker.position);
-
-                     {/if}
-                    {/foreach}
+    {if $settings.list_show_map}
 
-                    {if $settings.use_cluster_markers}
-                        var markerCluster = new MarkerClusterer(map, markers,
-                        {
-                            imagePath: '{$baseUrl}/js/googleMapsMarkerClusterer/images/m',
-                            gridSize: 30,
-                            maxZoom: 14,
-                            minimunClusterSize: 3
-                        });
-                    {/if}
+            /*
+             * Map operations
+             */
+             
+            var startLat = $('#glmLat').val();
+            var startLon = $('#glmLng').val();
+            var defZoom = Number({$settings.maps_default_zoom});
+            
+      {if $settings.selected_map_interface == 1}    
+                    
+            /*
+             *  Leaflet Map
+             *  API reference: https://leafletjs.com/reference-1.3.2.html
+             */
+
+            function initMap() {
+
+                var leafletMap = L.map('LeafletMapContainer').setView([{$settings.maps_default_lat}, {$settings.maps_default_lon}], defZoom);
+                var leafletTileServer = '{$settings.leaflet_tile_server}/{$settings.leaflet_tile_server_key}/' + {literal}'{z}/{x}/{y}.png'{/literal};
+                var leafletMinZoom = 3;
+                var leafletMaxZoom = 19;
+                var clusterRadiusMax = 40;
+                var geocoder;
+    
+                // Loading features
+                var loadingControl = L.Control.loading({
+                    separate: true,
+                    delayIndicator: 500
+                });
+                leafletMap.addControl(loadingControl);
+                
+                // Init Map
+                L.tileLayer(leafletTileServer, {
+                   attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.gaslightmedia.com/">Gaslight Media</a>',
+                   minZoom: leafletMinZoom,
+                   maxZoom: leafletMaxZoom,
+                   id: 'nothot'
+                }).addTo(leafletMap);
+    
+    
+            {if $mapItems}
+
+              {if $settings.use_cluster_markers}
+                var markerGroup = L.markerClusterGroup({
+                    maxClusterRadius: clusterRadiusMax
+                });
+              {else}
+                var markerGroup = L.featureGroup({
+                    maxClusterRadius: clusterRadiusMax
+                });
+              {/if}
+        
+                            
+              {foreach $mapItems as $m}
+                {if $m.lat != 0 && $m.lon != 0}
+    
+                // Create marker for this member and and to Feature Group 
+                var leafletMarker = L.marker([{$m.lat}, {$m.lon}], { title:  '{$m.member_name|escape:quotes}' })
+                    .bindPopup($('#map_info_' + {$m.id}).html())
+                    .addTo(markerGroup);
+    
+            {/if}
+              {/foreach}          
+    
+                leafletMap.addLayer(markerGroup);
+    
+                // Get outer bounds of all markers in the Feature Group
+                leafletMap.fitBounds(markerGroup.getBounds());               
+    
+            {/if} // havemembers
+    
+                // When a marker is clicked, display the pop-up near the center of the map.
+    
+                leafletMap.on('popupopen', function(e) {
+                    var px = leafletMap.project(e.popup._latlng);
+                    px.y -= e.popup._container.clientHeight/2;
+                    leafletMap.panTo(leafletMap.unproject(px),{ animate: true });
+                });
+    
 
-                        // Fit map to bounds of all markers
-                        map.fitBounds(bounds);
+            }
 
-                        // Check for first map idle (completely loaded) - Check max zoom
-                        var mapLoadedListener = google.maps.event.addListener(map, 'idle', function() {
-                            if (map.getZoom() > 14) {
-                                this.setZoom(14);
-                            }
-                            google.maps.event.removeListener(mapLoadedListener);
-                        });
+      {/if}
+            
+      {if $settings.selected_map_interface == 2}    
+    
+            /*
+             * Google Maps
+             *  API reference: https://developers.google.com/maps/documentation/javascript/reference
+             */
+    
+             function initMap() {
+    
+                // Create a Google Map object
+                var map = new google.maps.Map(document.getElementById('glm-locationMap'), {
+                        center: new google.maps.LatLng({$settings.maps_default_lat}, {$settings.maps_default_lon}),
+                        zoom: {$settings.maps_default_zoom},
+                    disableDefaultUI: false,
+                    mapTypeId: google.maps.MapTypeId.MAP,
+                });
+    
+                var center = map.getCenter();
+                google.maps.event.trigger(map, "resize");
+                map.setCenter(center);
+    
+                var currentScroll = $(window).scrollTop(); // get current position
+                var mapTop = $("#glm-locationMap-container").offset().top-10;
+                {if $settings.list_show_search}fixmeTop = $('#glm-member-list-filters-button').offset().top;{/if}
+                if (currentScroll >= mapTop) {
+                    $('body,html').animate({
+                    scrollTop: $("#glm-locationMap-container").offset().top-100
+                    }, 50);
+                } else {
+                    $('body,html').animate({
+                        scrollTop: $("#glm-locationMap-container").offset().top-100
+                    }, 400);
+               }
+    
+                var geocoder = new google.maps.Geocoder();
+                var bounds = new google.maps.LatLngBounds();
+                var infowindow = new google.maps.InfoWindow();
+        
+                // Don't Try HTML5 to get user geolocation
+                //if(navigator.geolocation) {
+                    //navigator.geolocation.getCurrentPosition(function(position) {
+                        //var myLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
+                        //var pinIcon = new google.maps.MarkerImage(
+                                //'https://maps.google.com/mapfiles/kml/shapes/man.png',
+                            //null, /* size is determined at runtime */
+                            //null, /* origin is 0,0 */
+                            //null, /* anchor is bottom center of the scaled image */
+                            //new google.maps.Size(30, 30)
+                        //);
+                        //var myMarker = new google.maps.Marker({
+                            //map: map,
+                            //position: myLocation,
+                            //draggable: false,
+                            //animation: google.maps.Animation.DROP,
+                            //title: 'My Location',
+                            //icon: pinIcon,
+                            //zIndex: 100
+                        //});
+                    //});
+                //}
+        
+    
+            {if $mapItems}
+                var markers = [];
+                {foreach $mapItems as $m}
+                  {if $m.lat != 0 && $m.lon != 0}
+    
+                    // Create a marker for this member
+                    var marker = new google.maps.Marker({
+                        map: map,
+                        position: new google.maps.LatLng({$m.lat}, {$m.lon}),
+                        draggable: false,
+                        animation: google.maps.Animation.DROP,
+                        title: '{$m.member_name|escape:quotes}',
+                        descr: $('#map_info_' + {$m.id}).html(),
+                        memberID: {$m.member}
+                    });
+    
+                    // Add a click listener for this marker
+                    marker.addListener('click', function() {
+                            infowindow.setOptions({
+                                    content: this.descr
+                            });
+                        infowindow.open(map,this);
+                    });
+    
+                    markers.push(marker);
+    
+                    // Extend the map bounds to include this marker
+                    bounds.extend(marker.position);
+    
+                 {/if}
+                {/foreach}
+    
+                {if $settings.use_cluster_markers}
+                    var markerCluster = new MarkerClusterer(map, markers,
+                    {
+                        imagePath: '{$baseUrl}/js/googleMapsMarkerClusterer/images/m',
+                        gridSize: 30,
+                        maxZoom: 14,
+                        minimunClusterSize: 3
+                    });
+                {/if}
+    
+                    // Fit map to bounds of all markers
+                    map.fitBounds(bounds);
+    
+                    // Check for first map idle (completely loaded) - Check max zoom
+                    var mapLoadedListener = google.maps.event.addListener(map, 'idle', function() {
+                        if (map.getZoom() > 14) {
+                            this.setZoom(14);
+                        }
+                        google.maps.event.removeListener(mapLoadedListener);
+                    });
 
-                {else}
+             } // initMap
+                    
+        {else}
 
 
-                {/if} // havemembers
+        {/if} // havemembers
 
+      {/if}                
 
-            {/if}   // settings.list_show_map
+    {/if}   // settings.list_show_map
 
+            // Handle Map View/Hide
+            var mapInitialized = false;
+            $("#glm-member-list-map-toggle").click( function() {
+                $(".glm-member-db-{$view}-view").toggleClass("map-opened");
+                $(".glm-member-db-{$view}-view").toggleClass("map-closed");
+                if (!mapInitialized) {
+                    initMap();
+                    mapInitialized = true;
+                }
+            });
+        {if $settings.list_map_show_opened}
+            // Start with map opened
+            initMap();
+            mapInitialized = true;
+        {/if}
+            
             // Processes click-through counts for website links
             $('.glm-member-list-website-link').on('click', function() {
 
             if( $(window).width() < 640 ){
                 $("#glm-member-list-filters-close").trigger("click");
             }
-        });
-        
+        });  
             </script>
 
         </div><!-- glm-member-list-inner-wrapper-->