Changed to using OpenStreetMaps Nominatim for geolocation services whe using Leaflet...
authorChuck Scott <cscott@gaslightmedia.com>
Mon, 27 Aug 2018 17:53:27 +0000 (13:53 -0400)
committerChuck Scott <cscott@gaslightmedia.com>
Mon, 27 Aug 2018 17:53:27 +0000 (13:53 -0400)
views/admin/member/memberInfo.html
views/admin/member/memberInfo/editProfileAddress.html

index c9001d2..31e3894 100755 (executable)
@@ -3,8 +3,6 @@
     {if $settings.selected_map_interface == 1}
         <link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.3/dist/leaflet.css" integrity="sha512-Rksm5RenBEKSKFjgI3a41vrjkw4EVPlJ3+OiI65vTjIdo9brlAacEuKOiQ5OFh7cOI1bkDwLqdLw3Zg0cRJAAQ==" crossorigin=""/>
         <script src="https://unpkg.com/leaflet@1.3.3/dist/leaflet.js" integrity="sha512-tAGcCfR4Sc5ZP5ZoVz0quoZDYX5aCtEm/eu1KhSLj2c9eFrylXZknQYmxUssFaVJKvvc0dJQixhGjG2yXWiV9Q==" crossorigin=""></script>
-        <!-- Use Google for Geocoding till we build a geocoding server -->
-        <script src="//maps.googleapis.com/maps/api/js?&key="></script>
     {/if}
     {if $settings.selected_map_interface == 2}
         <script src="//maps.googleapis.com/maps/api/js?&key={$settings.google_maps_api_key}"></script>
index 6ca6a21..37de80d 100755 (executable)
@@ -43,9 +43,7 @@
                                 <select name="city" id="city" class="glm-left">
                                     <option value="0"></option>
                                     {foreach from=$memberInfo.fieldData.city.list item=v}
-                                        <option value="{$v.value}"{if $v.default} selected="selected"{/if}>
-                                            {$v.name}
-                                        </option>
+                                        <option value="{$v.value}"{if $v.default} selected="selected"{/if}>{$v.name}</option>
                                     {/foreach}
                                 </select>
                             </div>
                     $('#glmLng').val(position.lng.toFixed(6));
                 });
 
-
-
-                // Temporarily use Google geolocation till we have our own geolocation server
-                // When estimate location button is clicked, geocode using address
+                /*
+                 * Use OpenStreetMaps Nominatim for Geolocation
+                 */
+                var nominatimAPI = "https://nominatim.openstreetmap.org/search/";
                 $(document).on('click','#glm-estimate-location', function() {
 
-                    var geocoder = new google.maps.Geocoder();
-
-                    // Get all address parts
-                    var geoAddr1 = $('#addr1').val().trim();
-                    var geoAddr2 = $('#addr2').val().trim();
-                    var geoCity = $('#city').find('option:selected').text().trim();
-                    var geoState = $('#state').find('option:selected').text().trim();
-                    var geoZIP = $('#zip').val().trim();
-                    var geoCountry = $('#country').find('option:selected').text().trim();
-
-                    // Assemble address string for
-                    var geoAddress = geoAddr1 + ', ' + geoAddr2 + ', ' + geoCity + ', ' + geoState + ' ' + geoZIP + ', ' + geoCountry;
-
-                    // Send to Google Geocoder
-                    geocoder.geocode( { 'address': geoAddress }, function(results, status) {
-
-                        // If we have a geocode solution
-                        if (status == google.maps.GeocoderStatus.OK) {
-
+                    var addrStreet = $('#addr1').val();
+                    var addrCity = $('#city option:selected').text();
+                    var addrState = $('#state').val();
+                    var addrCountry = $('#country').val();
+                    var addrZip = $('#zip').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 = results[0].geometry.location.lat();
-                            glmLng = results[0].geometry.location.lng();
+                            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));
 
-                        // Otherwise tell the user.
-                        } else {
-                            alert('Not able to estimate position from the current address.');
                         }
-                    });
 
+                    });         
+                
                 });
 
                 // Recenter when Update Poineter after entering Lat/Lon is clicked