Added second check when maping address to a location without city name if with city...
authorChuck Scott <cscott@gaslightmedia.com>
Thu, 25 Oct 2018 20:59:46 +0000 (16:59 -0400)
committerChuck Scott <cscott@gaslightmedia.com>
Thu, 25 Oct 2018 20:59:46 +0000 (16:59 -0400)
views/admin/member/memberInfo/editProfileAddress.html

index c42da86..78581d2 100755 (executable)
                  * Use OpenStreetMaps Nominatim for Geolocation
                  */
                 var nominatimAPI = "https://nominatim.openstreetmap.org/search/";
+               var addressMapFail = 'Sorry, we were unable to get a map position from the address provided.';
                 $(document).on('click','#glm-estimate-location', function() {
 
                     var addrStreet = $('#addr1').val();
                     var addrCountry = $('#country').val();
                     var addrZip = $('#zip').val();
 
+                    var haveLocation = false;
+
                     var location = $.getJSON( nominatimAPI, {
                         format: 'json',
                         street: addrStreet,
                         postalcode: addrZip
                     })
                     .fail(function(data) {
-                        alert('Sorry, we were unable to get a location from the above address.');                        
+                        alert(addressMapFail);
                     })
                     .done(function( data ) {
-
                         if( !data[0] || !data[0].lat || !data[0].lon ) {
-                            alert('Sorry, we were unable to get a location from the above address.');
+                            // Not found, try again without city name
+                            // Nominatim doesn't always find with the US ZIP preferred city name
+                            var location = $.getJSON( nominatimAPI, {
+                                format: 'json',
+                                street: addrStreet,
+                                // city: addrCity,
+                                state: addrState,
+                                country: addrCountry,
+                                postalcode: addrZip
+                            })
+                            .fail(function(data) {
+                                alert('Sorry, we had a communications failure. 2');
+                            })
+                            .done(function( data ) {
+                                if( !data[0] || !data[0].lat || !data[0].lon ) {
+                                    alert(addressMapFail);
+                                } else {
+                                    assignNewPosition(data);        
+                                    alert('We were unable to match all of your address data but did find this location.\nPlease check that it is correct and adjust as needed by dragging the map pointer.');
+                                }
+                            });
                         } else {
-        
-                            // Assign the new position to the hidden fields for submission
-                            glmLat = Number(data[0].lat);
-                            glmLng = Number(data[0].lon);
-                            $('#glmLat').val(glmLat.toFixed(6));
-                            $('#glmLng').val(glmLng.toFixed(6));
-
-                            // Move map pointer to the proper location
-                            leafletMarker.setLatLng(new L.LatLng(glmLat, glmLng),{ draggable:'true' });
-                            leafletMap.panTo(new L.LatLng(glmLat, glmLng));
-
+                                assignNewPosition(data);        
                         }
-
                     });         
                 
                 });
 
+                // Assign a position found by the Nominatim search above
+                function assignNewPosition(data) {
+
+                    // Assign the new position to the hidden fields for submission
+                    glmLat = Number(data[0].lat);
+                    glmLng = Number(data[0].lon);
+                    $('#glmLat').val(glmLat.toFixed(6));
+                    $('#glmLng').val(glmLng.toFixed(6));
+
+                    // Move map pointer to the proper location
+                    leafletMarker.setLatLng(new L.LatLng(glmLat, glmLng),{ draggable:'true' });
+                    leafletMap.panTo(new L.LatLng(glmLat, glmLng));
+
+                }
+
                 // Recenter when Update Poineter after entering Lat/Lon is clicked
                 $(document).on('click','#latLonRecenter', function() {
                     var glmLat = $('#glmLat').val();