When an address doesn't return a location, now also trying without the city name.
authorChuck Scott <cscott@gaslightmedia.com>
Fri, 26 Oct 2018 14:52:53 +0000 (10:52 -0400)
committerChuck Scott <cscott@gaslightmedia.com>
Fri, 26 Oct 2018 14:52:53 +0000 (10:52 -0400)
Nominatim dosn't check the "preferred" city name from the US ZIP database,
but can often find the address without the city name.

views/admin/events/edit.html

index cb70d7a..5ea9f18 100755 (executable)
                     });
 
                     // When estimate location button is clicked, geocode using address
+                    var addressMapFail = 'Sorry, we were unable to get a map position from the address provided.';
                     $('#glm-estimate-location_' + locID).on('click', function() {
 
                         var addrStreet = $('#address_' + locID).val();
                             postalcode: addrZip
                         })
                         .fail(function(data) {
-                            alert('Sorry, we were unable to get a location from the above address.');
+                            alert('Sorry, we had a communications failure. 2');
                         })
                         .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 {
+                                        console.log(data[0]);                                        
+                                        assignNewPosition(data);
+                                        alert('We were unable to match all of your address data but did find this location.\n\n' + data[0].display_name + '\n\nPlease check that it is correct and adjust as needed by dragging the map pointer.\nYou may also want to check the city selected in the address.');
+                                    }
+                                });
                             } else {
+                                assignNewPosition(data);
+                            }
+                            
+                        });
 
-                                // Assign the new position to the hidden fields for submission
-                                glmLat = Number(data[0].lat);
-                                glmLng = Number(data[0].lon);
-                                $('#glmLat_' + locID).val(glmLat.toFixed(6));
-                                $('#glmLng_' + locID).val(glmLng.toFixed(6));
+                    });
 
-                                // Move map pointer to the proper location
-                                leafletMarker.setLatLng(new L.LatLng(glmLat, glmLng),{ draggable:'true' });
-                                map[locID].panTo(new L.LatLng(glmLat, glmLng));
+                    // 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_' + locID).val(glmLat.toFixed(6));
+                        $('#glmLng_' + locID).val(glmLng.toFixed(6));
 
-                        });
+                        // Move map pointer to the proper location
+                        leafletMarker.setLatLng(new L.LatLng(glmLat, glmLng),{ draggable:'true' });
+                        map[locID].panTo(new L.LatLng(glmLat, glmLng));
 
-                    });
+                    }
 
                 }
             {/if}