diff --git a/app/views/neighborhoodMap.ejs b/app/views/neighborhoodMap.ejs index ce9d642..185c920 100644 --- a/app/views/neighborhoodMap.ejs +++ b/app/views/neighborhoodMap.ejs @@ -38,68 +38,13 @@ }; function initMap() { - map = new google.maps.Map(document.getElementById('map'), { - center: { lat: -34.397, lng: 150.644 }, - zoom: 11, - restriction: { - latLngBounds: BOSNIA_BOUNDS, - strictBounds: false, - }, - }); var geocoder = new google.maps.Geocoder(); document.getElementById('submit').addEventListener('click', function () { geocodeAddress(geocoder, map, false, latLngRestrictions); }); - function geocodeAddress(geocoder, resultsMap, isInit, geocoderRestrictions) { - var address = document.getElementById('address').value; - let geocoderOptions = geocoderRestrictions - ? { 'address': address, 'bounds': geocoderRestrictions } - : { 'address': address } - geocoder.geocode(geocoderOptions, function (results, status) { - if (status === 'OK') { - - var bounds = results[0].geometry.bounds; - - var resultBounds = new google.maps.LatLngBounds( - - results[0].geometry.viewport.getSouthWest(), - results[0].geometry.viewport.getNorthEast() - ); - - // map.fitBounds(resultBounds); - resultsMap.setCenter(results[0].geometry.location); - - if (isInit) { - - latLngRestrictions = new google.maps.LatLngBounds( - new google.maps.LatLng(bounds.getSouthWest().lat(), bounds.getSouthWest().lng()), - new google.maps.LatLng(bounds.getNorthEast().lng(), bounds.getNorthEast().lng())); - - - let latLngRestrictionsa = { - west: bounds.getSouthWest().lng(), - east: bounds.getNorthEast().lng(), - north: bounds.getNorthEast().lat(), - south: bounds.getSouthWest().lat() - } - map.setOptions({ - restriction: { - latLngBounds: latLngRestrictionsa, - strictBounds: false, - } - }) - } else { - resultsMap.setZoom(17); - } - - } else { - alert('Geocode was not successful for the following reason: ' + status); - } - }); - } defaultAddress.value = municipality; geocodeAddress(geocoder, map, true); @@ -119,6 +64,65 @@ } + + function geocodeAddress(geocoder, resultsMap, isInit, geocoderRestrictions) { + + + + var address = document.getElementById('address').value; + let geocoderOptions = geocoderRestrictions + ? { 'address': address, 'bounds': geocoderRestrictions } + : { 'address': address } + + geocoder.geocode(geocoderOptions, function (results, status) { + if (status === 'OK') { + + var bounds = results[0].geometry.bounds; + + var resultBounds = new google.maps.LatLngBounds( + + results[0].geometry.viewport.getSouthWest(), + results[0].geometry.viewport.getNorthEast() + ); + + if (isInit) { + map = new google.maps.Map(document.getElementById('map'), { + zoom: 11, + }); + resultsMap = map + } + + // map.fitBounds(resultBounds); + resultsMap.setCenter(results[0].geometry.location); + + if (isInit) { + + latLngRestrictions = new google.maps.LatLngBounds( + new google.maps.LatLng(bounds.getSouthWest().lat(), bounds.getSouthWest().lng()), + new google.maps.LatLng(bounds.getNorthEast().lng(), bounds.getNorthEast().lng())); + + + let latLngRestrictionsa = { + west: bounds.getSouthWest().lng(), + east: bounds.getNorthEast().lng(), + north: bounds.getNorthEast().lat(), + south: bounds.getSouthWest().lat() + } + map.setOptions({ + restriction: { + latLngBounds: latLngRestrictionsa, + strictBounds: false, + } + }) + } else { + resultsMap.setZoom(17); + } + + } else { + alert('Geocode was not successful for the following reason: ' + status); + } + }); + }