I have searched everywhere on stack overflow for a solution for this while there are similar issues and questions out there none of them address my specific issue.
For months this google maps page i have was working but suddenly started just displaying a blank map.
Here is what the code previously looked like that was working up until today
@using something.ViewModel
@model IGoogleMapsCapable
<link href="@Html.ContentVersioned("/Content/GoogleMapStyle.css")" rel="stylesheet" type="text/css" />
<script src="@Html.ContentVersioned("/Scripts/google-maps.js")"></script>
@Html.HiddenFor(x => x.GoogleMaps.OriginalLat)
@Html.HiddenFor(x => x.GoogleMaps.OriginalLng)
@Html.HiddenFor(x => x.GoogleMaps.NewLat)
@Html.HiddenFor(x => x.GoogleMaps.NewLng)
<div class="alert alert-danger" role="alert">
<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
<span class="sr-only">The current Latitude/Longitude is not considered accurate enough to bind the policy. Please enter a new Latitude/Longitude here</span>
<p>The current Latitude/Longitude is not considered accurate enough to bind the policy. Please enter a new Latitude/Longitude <a class="mimic-hyperlink-text" href="#modalGoogleMap" data-toggle="modal" data-target="#modalGoogleMap">here</a></p>
</div>
<div class="modal fade in" id="modalGoogleMap" tabindex="-1" role="dialog" aria-labelledby="modalGoogleMap" data-backdrop="static" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" id="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<p class="modal-title" id="red-txt">Geolocation did not return as rooftop. Please drag and drop red pin on home location</p>
</div>
<div class="modal-body row-fluid">
<div class="well">
<div id="map"></div>
</div>
<div class="well">
<div id="lngLat">
<input type="text" class="lngText" id="lngText" aria-label="Longitude"/>
<input type="text" class="latText" id="latText" aria-label="Latitude"/>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" name="modalGoogleMapClose" id="modalGoogleMapClose" class="btn btn-primary">Update</button>
<button type="button" name="modalGoogleMapCancel" id="modalGoogleMapCancel" class="btn" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
and the jquery
(function ($) {
$(document).ready(function () {
let map;
let marker;
if (true)
{
$('#modalGoogleMap').modal('show');
}
originalLatLng = {
lat: parseFloat($('#GoogleMaps_OriginalLat').val()),
lng: parseFloat($('#GoogleMaps_OriginalLng').val())
};
newLatLng = {
lat: parseFloat($('#GoogleMaps_NewLat').val()),
lng: parseFloat($('#GoogleMaps_NewLng').val())
};
function markerUpdate(map) {
marker.setMap(null);
debugger
newLatLng.lat = parseFloat(document.getElementById("latText").value);
newLatLng.lng = parseFloat(document.getElementById("lngText").value);
marker = new google.maps.Marker({
position: newLatLng,
map,
title: 'Map Pin Updated: Manual Input',
draggable: true
});
marker.setMap(map);
}
function addMarkerListener(map, marker) {
map = new google.maps.event.addListener(marker, 'dragend', function (evt) {
newLatLng.lat = evt.latLng.lat().toFixed(6);
newLatLng.lng = evt.latLng.lng().toFixed(6);
$('#lngText').val(newLatLng.lng);
$('#latText').val(newLatLng.lat);
});
}
function initMap() {
map = new google.maps.Map(document.getElementById("map"), {
center: originalLatLng,
zoom: 18
});
marker = new google.maps.Marker({
position: originalLatLng,
map,
title: 'Rooftop of Home to be Insured',
draggable: true
});
addMarkerListener(map, marker);
debugger
$('#lngText').val(originalLatLng.lng)
$('#latText').val(originalLatLng.lat)
document.getElementById("latText").oninput = function () { markerUpdate(map) };
document.getElementById("lngText").oninput = function () { markerUpdate(map) };
}
$('#modalGoogleMapClose').click(function () {
GetNewLocation();
});
$('#modalGoogleMapCancel').click(function () {
initMap();
})
$('#close').click(function () {
initMap();
})
$("#GeoInformation").validate({
submitHandler: function (form) {
let isSuccess = GetNewLocation();
if (isSuccess) {
form.submit();
}
},
errorPlacement: function (error, element) {
error.insertBefore(element);
},
onkeyup: false,
onclick: false
});
// Create the script tag, set the appropriate attributes
var script = document.createElement('script');
script.src = 'https://maps.googleapis.com/maps/api/js?v=quarterly&key=MyActualKeyHere&callback=initMap';
script.async = true;
// Attach your callback function to the `window` object
window.initMap = function () {
// JS API is loaded and available
initMap();
};
// Append the 'script' element to 'head'
document.head.appendChild(script);
}); // End document ready
})(this.jQuery);
Please any help would be appreciated I have been breaking my head trying to figure this one out
Looking through google maps api js documentation ive tried their suggested changes https://developers.google.com/maps/documentation/javascript/load-maps-js-api
(function ($) {
$(document).ready(function () {
let gmap;
let marker;
if (true) {
$('#modalGoogleMap').modal('show');
}
let originalLatLng = {
lat: parseFloat($('#GoogleMaps_OriginalLat').val()),
lng: parseFloat($('#GoogleMaps_OriginalLng').val())
};
let newLatLng = {
lat: parseFloat($('#GoogleMaps_NewLat').val()),
lng: parseFloat($('#GoogleMaps_NewLng').val())
};
function markerUpdate(gmap) {
marker.setMap(null);
newLatLng.lat = parseFloat(document.getElementById("latText").value);
newLatLng.lng = parseFloat(document.getElementById("lngText").value);
marker = new google.maps.Marker({
gmap,
position: newLatLng,
title: 'Map Pin Updated: Manual Input',
draggable: true
});
marker.setMap(gmap);
}
function addMarkerListener(gmap, marker) {
gmap = new google.maps.event.addListener(marker, 'dragend', function (evt) {
newLatLng.lat = evt.latLng.lat().toFixed(6);
newLatLng.lng = evt.latLng.lng().toFixed(6);
$('#lngText').val(newLatLng.lng);
$('#latText').val(newLatLng.lat);
});
}
function initMap() {
try {
gmap = new google.maps.Map(document.getElementById("map"), {
center: originalLatLng,
zoom: 18
});
marker = new google.maps.Marker({
gmap,
position: originalLatLng,
title: 'Rooftop of Home to be Insured',
draggable: true
});
addMarkerListener(gmap, marker);
$('#lngText').val(originalLatLng.lng)
$('#latText').val(originalLatLng.lat)
document.getElementById("latText").oninput = function () { markerUpdate(gmap) };
document.getElementById("lngText").oninput = function () { markerUpdate(gmap) };
} catch (e) {
console.log(e);
}
}
$('#modalGoogleMapClose').click(function () {
GetNewLocation();
});
$('#modalGoogleMapCancel').click(function () {
initMap();
})
$('#close').click(function () {
initMap();
})
$("#GeoInformation").validate({
submitHandler: function (form) {
let isSuccess = GetNewLocation();
if (isSuccess) {
form.submit();
}
},
errorPlacement: function (error, element) {
error.insertBefore(element);
},
onkeyup: false,
onclick: false
});
initMap();
}); // End document ready
})(this.jQuery);
this is the html
@using some.ViewModel
@model IGoogleMapsCapable
<link href="@Html.ContentVersioned("/Content/GoogleMapStyle.css")" rel="stylesheet" type="text/css" />
<script>
(g => {
console.log("loading google maps");
var h, a, k, p = "The Google Maps JavaScript API", c = "google", l = "importLibrary",
q = "__ib__", m = document, b = window; b = b[c] || (b[c] = {}); var d = b.maps || (b.maps = {}),
r = new Set, e = new URLSearchParams, u = () => h || (h = new Promise(async (f, n) => {
await (a = m.createElement("script")); e.set("libraries", [...r] + ""); for (k in g) e.set(k.replace(/[A-Z]/g, t =>
"_" + t[0].toLowerCase()), g[k]); e.set("callback", c + ".maps." + q);
a.src = `https://maps.${c}apis.com/maps/api/js?` + e; d[q] = f; a.onerror = () =>
h = n(Error(p + " could not load.")); a.nonce = m.querySelector("script[nonce]")?.nonce || ""; m.head.append(a)
}));
d[l] ? console.warn(p + " only loads once. Ignoring:", g) : d[l] = (f, ...n) => r.add(f) && u().then(() =>
d[l](f, ...n))
console.log("google maps loaded", h, a, k, p);
})
({
key: "I have my actual key here",
v: "quarterly",
});
</script>
<script src="@Html.ContentVersioned("/Scripts/google-maps.js")"></script>
@Html.HiddenFor(x => x.GoogleMaps.OriginalLat)
@Html.HiddenFor(x => x.GoogleMaps.OriginalLng)
@Html.HiddenFor(x => x.GoogleMaps.NewLat)
@Html.HiddenFor(x => x.GoogleMaps.NewLng)
<div class="alert alert-danger" role="alert">
<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
<span class="sr-only">The current Latitude/Longitude is not considered accurate enough to bind the policy. Please enter a new Latitude/Longitude here</span>
<p>The current Latitude/Longitude is not considered accurate enough to bind the policy. Please enter a new Latitude/Longitude <a class="mimic-hyperlink-text" href="#modalGoogleMap" data-toggle="modal" data-target="#modalGoogleMap">here</a></p>
</div>
<div class="modal fade in" id="modalGoogleMap" tabindex="-1" role="dialog" aria-labelledby="modalGoogleMap" data-backdrop="static" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" id="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<p class="modal-title" id="red-txt">Geolocation did not return as rooftop. Please drag and drop red pin on home location</p>
</div>
<div class="modal-body row-fluid">
<div class="well">
<div id="map"></div>
</div>
<div class="well">
<div id="lngLat">
<input type="text" class="lngText" id="lngText" aria-label="Longitude" />
<input type="text" class="latText" id="latText" aria-label="Latitude" />
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" name="modalGoogleMapClose" id="modalGoogleMapClose" class="btn btn-primary">Update</button>
<button type="button" name="modalGoogleMapCancel" id="modalGoogleMapCancel" class="btn" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
I have tried a variety of things straight from the google maps javascript api documentation.
if i make the functions in the JS async and use await as it describes in the documentation I get a
Uncaught (in promise) TypeError: r is not iterable error
if i use legacy loading like this
<script async src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"> </script>
I get a blank map still but no errors in console
with the current code this is the caught error in the try catch from the initMap
`TypeError: GMap is not a constructor
at initMap (google-maps.js:60:24)
at HTMLDocument.<anonymous> (google-maps.js:182:9)
at e (jquery-3.4.1.min.js:2:29453)
at t (jquery-3.4.1.min.js:2:29755)`
I have also tried as per the documentation
async function initMap() {
const { Map } = await google.maps.importLibrary("maps");
map = new Map(document.getElementById("map"), {
center: { lat: -34.397, lng: 150.644 },
zoom: 8,
});
}
that results in this error
`Uncaught (in promise) TypeError: r is not iterable
`
on the const { Map } line
Ive even tried renaming Map to GMAp in that const since i believe Map is a reserved word.