I am trying to load this example into a C# ASP.Net server. I am getting an error message: Google maps marker.AdvancedMarkerElement is undefined
Reference Code:
- Report.cshtml
Note: Insert your API key
@*
For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
}
<div id="map"></div>
@section Scripts{
<script>
function initMap() {
const position = { lat: -25.344, lng: 131.031 };
const map = new google.maps.Map(document.getElementById("map"), {
zoom: 4,
center: position,
mapId: "DEMO_MAP_ID", // Map ID is required for advanced markers.
});
// The advanced marker, positioned at Uluru
const marker = new google.maps.marker.AdvancedMarkerElement({
map,
position: position,
title: 'Uluru',
});
};
$(document).ready(function () {
window.initMap = initMap;
});
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=xxxxxx&callback=initMap&v=weekly&loading=async"
defer>
</script>
}
- Controller
using FastTestMap.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
namespace FastTestMap.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
return View();
}
public IActionResult Privacy()
{
return View();
}
public IActionResult Report()
{
return View();
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}
I am trying to run the above example using the example provided on Google Advance Marker Element and I think the problem is with the way the library is getting loaded. I believe that the library is deferring its load, therefore the advance marker is undefined.
I tried the stackoverflow solution in comments to no end.
You need to add marker library
https://developers.google.com/maps/documentation/javascript/libraries