From ae9bbfecfb3a9d9c1587e17739cc6cd4e60aa7e1 Mon Sep 17 00:00:00 2001 From: Chuck Scott Date: Fri, 26 Oct 2018 10:52:53 -0400 Subject: [PATCH] When an address doesn't return a location, now also trying without the city name. 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 | 53 +++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 13 deletions(-) diff --git a/views/admin/events/edit.html b/views/admin/events/edit.html index cb70d7a..5ea9f18 100755 --- a/views/admin/events/edit.html +++ b/views/admin/events/edit.html @@ -1256,6 +1256,7 @@ }); // 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(); @@ -1273,29 +1274,55 @@ 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} -- 2.17.1