From c002eb574ecf1620b27f58f0700fa438dc888a28 Mon Sep 17 00:00:00 2001 From: Chuck Scott Date: Mon, 27 Aug 2018 14:34:15 -0400 Subject: [PATCH] Added geolocation to event location map and fixed other related issues. --- views/admin/events/edit.html | 141 ++++++++++++++++----------- views/admin/events/editLocation.html | 6 +- 2 files changed, 84 insertions(+), 63 deletions(-) mode change 100644 => 100755 views/admin/events/edit.html mode change 100644 => 100755 views/admin/events/editLocation.html diff --git a/views/admin/events/edit.html b/views/admin/events/edit.html old mode 100644 new mode 100755 index cea940f..07e07c3 --- a/views/admin/events/edit.html +++ b/views/admin/events/edit.html @@ -416,7 +416,9 @@ var glmLat = $('#glmLat_' + locID).val(); var glmLng = $('#glmLng_' + locID).val(); - location[locID] = new google.maps.LatLng(glmLat, glmLng); + {if $settings.selected_map_interface == 2} + location[locID] = new google.maps.LatLng(glmLat, glmLng); + {/if} initMap(locID); @@ -1191,8 +1193,7 @@ } /* - * Google Maps - * API reference: https://developers.google.com/maps/documentation/javascript/reference + * Mapping */ var map = new Object(); var geocoder = new Object(); @@ -1201,6 +1202,8 @@ {if $settings.selected_map_interface == 1} + var nominatimAPI = "https://nominatim.openstreetmap.org/search/"; + /* * Leaflet Map * API reference: https://leafletjs.com/reference-1.3.2.html @@ -1228,10 +1231,6 @@ id: 'nothot' }).addTo(map[locID]); - // Geocoder Object (temp use) - // TODO: Need replace from Google - geocoder[locID] = new google.maps.Geocoder(); - // Marker var leafletMarker = L.marker([startLat, startLon], { draggable: true @@ -1253,18 +1252,43 @@ // When estimate location button is clicked, geocode using address $('#glm-estimate-location_' + locID).on('click', function() { - // Stop any automatic updates - mapLocationAuto[locID] = false; + var addrStreet = $('#address_' + locID).val(); + var addrCity = $('#city_' + locID + ' option:selected').text(); + var addrState = $('#state_' + locID).val(); + var addrCountry = $('#country_' + locID).val(); + var addrZip = $('#zip_' + locID).val(); + + var location = $.getJSON( nominatimAPI, { + format: 'json', + street: addrStreet, + city: addrCity, + state: addrState, + country: addrCountry, + postalcode: addrZip + }) + .fail(function(data) { + alert('Sorry, we were unable to get a location from the above address.'); + }) + .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.'); + } else { + + // 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)); - glmGeocode(locID); - }); + } + + }); - // When an address is updated and mapLocationAuto is set for that locID, geocode using address - $('.location-address').on('change', function() { - locID = $(this).attr('data-id'); - if (mapLocationAuto[locID]) { - glmGeocode(locID); - } }); } @@ -1331,49 +1355,50 @@ }); } - {/if} - - function glmGeocode(locID) { - - // Get all address parts - var geoAddr1 = $('#address_' + locID ).val().trim(); - var geoAddr2 = ''; // $('#addr2').val().trim(); - var geoCity = $('#city_' + locID).find('option:selected').text().trim(); - var geoState = $('#state_' + locID).val(); - var geoZIP = $('#zip_' + locID).val().trim(); - var geoCountry = $('#country_' + locID).find('option:selected').text().trim(); - - // Assemble address string for - var geoAddress = geoAddr1 + ', ' + geoAddr2 + ', ' + geoCity + ', ' + geoState + ' ' + geoZIP + ', ' + geoCountry; - - // Send to Google Geocoder - geocoder[locID].geocode( { 'address': geoAddress }, function(results, status) { - // If we have a geocode solution - if (status == google.maps.GeocoderStatus.OK) { - - // Center the map and locate the marger to the new location - map[locID].setCenter(results[0].geometry.location); - marker[locID].setPosition( results[0].geometry.location ); - - // Assign the new position to the hidden fields for submission - glmLat = results[0].geometry.location.lat(); - glmLng = results[0].geometry.location.lng(); - $('#glmLat_' + locID).val(glmLat.toFixed(6)); - $('#glmLng_' + locID).val(glmLng.toFixed(6)); - - // Also display it to the user - $('#mapPosition_' + locID).html('Lat ' + glmLat.toFixed(4) + ', Lon ' + glmLng.toFixed(4)); - - // Otherwise tell the user. - } else { - alert('Not able to estimate map position from the current address.'); - } - }); - - - } + function glmGeocode(locID) { + + // Get all address parts + var geoAddr1 = $('#address_' + locID ).val().trim(); + var geoAddr2 = ''; // $('#addr2').val().trim(); + var geoCity = $('#city_' + locID).find('option:selected').text().trim(); + var geoState = $('#state_' + locID).val(); + var geoZIP = $('#zip_' + locID).val().trim(); + var geoCountry = $('#country_' + locID).find('option:selected').text().trim(); + + // Assemble address string for + var geoAddress = geoAddr1 + ', ' + geoAddr2 + ', ' + geoCity + ', ' + geoState + ' ' + geoZIP + ', ' + geoCountry; + + // Send to Google Geocoder + geocoder[locID].geocode( { 'address': geoAddress }, function(results, status) { + + // If we have a geocode solution + if (status == google.maps.GeocoderStatus.OK) { + + // Center the map and locate the marger to the new location + map[locID].setCenter(results[0].geometry.location); + marker[locID].setPosition( results[0].geometry.location ); + + // Assign the new position to the hidden fields for submission + glmLat = results[0].geometry.location.lat(); + glmLng = results[0].geometry.location.lng(); + $('#glmLat_' + locID).val(glmLat.toFixed(6)); + $('#glmLng_' + locID).val(glmLng.toFixed(6)); + + // Also display it to the user + $('#mapPosition_' + locID).html('Lat ' + glmLat.toFixed(4) + ', Lon ' + glmLng.toFixed(4)); + + // Otherwise tell the user. + } else { + alert('Not able to estimate map position from the current address.'); + } + }); + + + } + {/if} + /* * Misc support items */ diff --git a/views/admin/events/editLocation.html b/views/admin/events/editLocation.html old mode 100644 new mode 100755 index cfe0234..55f5d94 --- a/views/admin/events/editLocation.html +++ b/views/admin/events/editLocation.html @@ -4,8 +4,6 @@ {if $settings.selected_map_interface == 1} - - {/if} {if $settings.selected_map_interface == 2} @@ -262,9 +260,7 @@ -- 2.17.1