Fixed map not loding bug
This commit is contained in:
@@ -38,68 +38,13 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
function initMap() {
|
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();
|
var geocoder = new google.maps.Geocoder();
|
||||||
|
|
||||||
document.getElementById('submit').addEventListener('click', function () {
|
document.getElementById('submit').addEventListener('click', function () {
|
||||||
geocodeAddress(geocoder, map, false, latLngRestrictions);
|
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;
|
defaultAddress.value = municipality;
|
||||||
geocodeAddress(geocoder, map, true);
|
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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAna8ohfV2HBMcxGk_29vqxU5Z_bDickqg&callback=initMap" async
|
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAna8ohfV2HBMcxGk_29vqxU5Z_bDickqg&callback=initMap" async
|
||||||
defer></script>
|
defer></script>
|
||||||
|
|||||||
Reference in New Issue
Block a user