Working on making sure that all functions work for the NearMe map
authorChuck Scott <cscott@gaslightmedia.com>
Tue, 13 Dec 2016 20:57:33 +0000 (15:57 -0500)
committerChuck Scott <cscott@gaslightmedia.com>
Tue, 13 Dec 2016 20:57:33 +0000 (15:57 -0500)
  Fixed not pushing map postion when going to all area search
  Fixed when poi processing is delayed and not delayed.

views/front/nearme/index.html

index a296762..c66a20d 100644 (file)
@@ -75,7 +75,7 @@
             var highAccuracyPoisition = false;  // Request high-accuracy user geolocation from user's device
             var postionTimeout = 15000;         // Maximum amount of time we'll wait for geolocation result - 15 Sec
             var geolocationFailCount = 2;       // Allow geolocation to fail this many times before switching to manual location settings ('set')
-            var trackToConsole = false;          // Send debug/progress messages to developers console (Firefox)
+            var trackToConsole = true;          // Send debug/progress messages to developers console (Firefox)
             
             // Set default - Need to make this configurable
             var mapLocation = new google.maps.LatLng(mapLat, mapLon);
             var getLocationIngoredTimer;
             var geolocationFailCounter = 0;
             var stopGeolocation = false;
+            var noProcessAfterZoom = false;
                         
             function initMap() {
                 
                     // Close map bubble if used
                     closeMapBubble();
                     
-                    checkNewPOIs();
+                    // Check if we need to get new POIs after this zoom - Dissabled for "everywhere" search
+                    if (!noProcessAfterZoom) {
+                        checkNewPOIs();
+                    } else {
+                        if (trackToConsole) { console.log('GLM NearMe: Not calling checkNewPOIs() for this zoom'); }
+                        noProcessAfterZoom = false;
+                    }
+                    
                 });
                 
             }
 
-            // Watch for Reference Map Position request by user
+            // Return to My Location
             $('#glmMapGoToMyLocation').on('click', function() {
                 
                 if (trackToConsole) { console.log('GLM NearMe: User GoToMyLocation clicked'); }
                 
-                goToPosition(myMarker.getPosition());
-                return false;
-            });
-
-            function goToPosition(position) {
-                map.setCenter(position);
+                noProcessAfterZoom = true;
+                
+                // Go to the user's saved postion and default zoom
+                map.setCenter(myMarker.getPosition());
                 map.setZoom(defZoom)
-                checkNewPOIs();
-            }
-            
+                
+                // Clear all POIs and run check for new ones.
+                checkNewPOIs(boundsTimeout, true);
+                
+            });
             
-            // Watch for request to set My Selected Location to center of map
+            // Set My Selected Location to center of map
             $('#glmMapSetMyLocation').on('click', function() {
                 
                 if (trackToConsole) { console.log('GLM NearMe: User SetMyLocation clicked'); }
                 
                 myMarker.setPosition(map.getCenter());
-                return false;
             });
             
-            // Watch for Previous Map Position request by user
+            // Previous Map Position request by user
             $('#glmMapBack').on('click', function() {
                 
                 if (trackToConsole) { console.log('GLM NearMe: User MapBack clicked'); }
                 return false;
             });
             
-            // Redo search when Search Anywhere button is clicked.
+            // Search Anywhere button is clicked.
             $('#glmNearMeSearchAnywhere').click('change', function() {
+                
+                // Using getBoundsPOIs here because it's awkward to wait when a button is clicked
                 getBoundsPOIs(true, true);
+                
             });
 
-            // Trigger text search for POIs when search text is changed
+            // Text search requested
             $('#glmNearMeSearchText').catchEnter().on('enterkey', function() {
-                getBoundsPOIs(true);
+                checkNewPOIs(boundsTimeout, true);
             });
 
             // Schedule a check for new POIs
-            function checkNewPOIs(timeout = boundsTimeout) {
+            function checkNewPOIs(timeout = boundsTimeout, resetPOIs = false, anywhere = false) {
 
                 if (trackToConsole) { console.log('GLM NearMe: checkNewPOIs()'); }
                 
                 window.clearTimeout(boundsDelayTimer);
                 
                 // Set timer to get POIs
-                boundsDelayTimer = window.setTimeout(getBoundsPOIs, timeout);
+                boundsDelayTimer = window.setTimeout(getBoundsPOIs, timeout, resetPOIs, anywhere);
 
             }
             
                         setMyLocationToManual();
                     }
                     
-                    // Then turn off checked
-                    $('#glmNearMeSearchAnywhere').attr("checked", false);
                 }
 
                 
                     dontPushPos = false;
                 } else {
                     
+                    if (trackToConsole) { console.log('GLM NearMe: Pushing location on stack'); }
+                    
                     // Push the previous position on the stack
                     mapViewStack.push({
                         zoom: curZoom,
                     // Show the back button
                     $('#glmMapBack').removeClass('glm-hidden');
                     
-                    // Clear the don't push on stack flag
-                    dontPushPos = false;
                 }
                 
                 // Request any POIs in new areas of the map
                     function( newPOIs ) {
                         
                         if (trackToConsole) { console.log('GLM NearMe: Requesting newPOIs'); }
-                        
+
+                        if (trackToConsole) { console.log('GLM NearMe: Number of markers = '+Object.keys(markers).length); }
+
                         // If resetPOIs is requested, clear all clusterer markers, all map markers, and our own markers array.
                         if (resetPOIs) {
                             
                             markers = [];
 //                            markerClusterer.redraw();
                             
+                            if (trackToConsole) { console.log('GLM NearMe: Clearing all markers.'); }
                             
                         // Otherwise remove only markers that are off our map
                         } else {
     
                             }                        
     
-                            if (trackToConsole) { console.log('GLM NearMe: '+Object.keys(delQueue).length+' markers in delete queue'); }
+                            if (trackToConsole) { console.log('GLM NearMe: Markers in delete queue = '+Object.keys(delQueue).length); }
 
                             // Remove markers in the delete queue from the clusterer and markers array - can't do this in loop above
+                            var deleted = 0; 
                             if (Object.keys(delQueue).length) {
                                 $.each( delQueue, function(key, value) {               
                                     
                                     // Delete our marker
                                     delete markers[value];
                                     
+                                    deleted++;
+                                    
                                 });
                             }
                             delQueue = [];  // Clear delete queue 
+
+                            if (trackToConsole) { console.log('GLM NearMe: Number of markers deleted = '+deleted); }
                             
                         }
                         
+if (trackToConsole) { console.log('GLM NearMe: Number of markers remaining = '+Object.keys(markers).length); }
+                        
+                        
                         if (trackToConsole) { console.log('GLM NearMe: Processing '+Object.keys(newPOIs.sources).length+' POI sources'); }
                         
                         // Add all sources (members, events, ...) to the assignedPushPins array
                         // If there's a request to find things anywhere
                         if (request == 'anywhere' && Object.keys(markers).length) {
                             
+                            noProcessAfterZoom = true;
+                            
                             allBounds  = new google.maps.LatLngBounds();
                             
                             // Find the total extent of all current markers.
                             if (zoom > maxAnywhereZoom) {
                                 map.setZoom(maxAnywhereZoom);                        
                             }
-
+                            
+                            saveCurrentBounds();
                             
                         }
 
                     }
                 );
                 
-                // Save current bounds as last bounds
+                saveCurrentBounds();
+                    
+            }
+
+            // Save current bounds as last bounds
+            function saveCurrentBounds() {
+
+                if (trackToConsole) { console.log('GLM NearMe: Set Last Bounds'); }
+                
                 lastLatMax = latMax;
                 lastLatMin = latMin;
                 lastLonMax = lonMax;
                 lastLonMin = lonMin;
 
-                // Save the current center position and zoom to push on stack before next move
+                // Save the current center position and save to push on stack before next move
                 curZoom = map.getZoom();
                 curCenter = map.getCenter();
 
             }
-
+            
+            
             // Handle tracking type selection
             $('.glmMapMyLocationType').on('change', function() {
                 
                         // If current location is not on map, recenter map on location
                         if (!map.getBounds().contains(myMarker.getPosition())) {
                             map.setCenter(myCurrentLocation);    
-                            getBoundsPOIs();
+                            checkNewPOIs();
                         }
         
                     // If we can't get the location, then switch to manual