From 42c75f23815c2358cda8d9184cb40cdd04163a36 Mon Sep 17 00:00:00 2001 From: Chuck Scott Date: Thu, 25 Oct 2018 16:59:46 -0400 Subject: [PATCH] Added second check when maping address to a location without city name if with city fails. --- .../member/memberInfo/editProfileAddress.html | 56 ++++++++++++++----- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/views/admin/member/memberInfo/editProfileAddress.html b/views/admin/member/memberInfo/editProfileAddress.html index c42da86b..78581d2f 100755 --- a/views/admin/member/memberInfo/editProfileAddress.html +++ b/views/admin/member/memberInfo/editProfileAddress.html @@ -199,6 +199,7 @@ * 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(); @@ -207,6 +208,8 @@ var addrCountry = $('#country').val(); var addrZip = $('#zip').val(); + var haveLocation = false; + var location = $.getJSON( nominatimAPI, { format: 'json', street: addrStreet, @@ -216,30 +219,53 @@ 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(); -- 2.17.1