我正在使用ASP.Net网络应用程序,我正在尝试在我的网站上查看谷歌地图,但无论我在哪里运行网站,我都会收到此错误:
此页面无法显示Google地图元素。提供商 Google API密钥无效或此网站无权使用它。 错误代码:InvalidKeyOrUnauthorizedURLmapError
源代码:
!DOCTYPE html>
<html>
<head>
<link href="../assets/styles.min.css" rel="stylesheet">
<title>Google Maps: Latitude-Longitude Finder Tool</title>
<style>
#map-search { position: absolute; top: 10px; left: 10px; right: 10px; }
#search-txt { float: left; width: 60%; }
#search-btn { float: left; width: 19%; }
#detect-btn { float: right; width: 19%; }
#map-canvas { position: absolute; top: 40px; bottom: 65px; left: 10px; right: 10px; }
#map-output { position: absolute; bottom: 10px; left: 10px; right: 10px; }
#map-output a { float: right; }
</style>
</head>
<body>
<div id="map-search">
<input id="search-txt" type="text" value="Disneyland, 1313 S Harbor Blvd, Anaheim, CA 92802, USA" maxlength="100">
<input id="search-btn" type="button" value="Locate Address">
<input id="detect-btn" type="button" value="Detect Location" disabled>
</div>
<div id="map-canvas"></div>
<div id="map-output"></div>
<script type="text/javascript">
/*
* Google Maps: Latitude-Longitude Finder Tool
* http://salman-w.blogspot.com/2009/03/latitude-longitude-finder-tool.html
*/
function loadmap() {
// initialize map
var map = new google.maps.Map(document.getElementById("map-canvas"), {
center: new google.maps.LatLng(33.808678, -117.918921),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
// initialize marker
var marker = new google.maps.Marker({
position: map.getCenter(),
draggable: true,
map: map
});
// intercept map and marker movements
google.maps.event.addListener(map, "idle", function() {
marker.setPosition(map.getCenter());
document.getElementById("map-output").innerHTML = "Latitude: " + map.getCenter().lat().toFixed(6) + "<br>Longitude: " + map.getCenter().lng().toFixed(6) + "<a href='https://www.google.com/maps?q=" + encodeURIComponent(map.getCenter().toUrlValue()) + "' target='_blank'>Go to maps.google.com</a>";
});
google.maps.event.addListener(marker, "dragend", function(mapEvent) {
map.panTo(mapEvent.latLng);
});
// initialize geocoder
var geocoder = new google.maps.Geocoder();
google.maps.event.addDomListener(document.getElementById("search-btn"), "click", function() {
geocoder.geocode({ address: document.getElementById("search-txt").value }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var result = results[0];
document.getElementById("search-txt").value = result.formatted_address;
if (result.geometry.viewport) {
map.fitBounds(result.geometry.viewport);
} else {
map.setCenter(result.geometry.location);
}
} else if (status == google.maps.GeocoderStatus.ZERO_RESULTS) {
alert("Sorry, geocoder API failed to locate the address.");
} else {
alert("Sorry, geocoder API failed with an error.");
}
});
});
google.maps.event.addDomListener(document.getElementById("search-txt"), "keydown", function(domEvent) {
if (domEvent.which === 13 || domEvent.keyCode === 13) {
google.maps.event.trigger(document.getElementById("search-btn"), "click");
}
});
// initialize geolocation
if (navigator.geolocation) {
google.maps.event.addDomListener(document.getElementById("detect-btn"), "click", function() {
navigator.geolocation.getCurrentPosition(function(position) {
map.setCenter(new google.maps.LatLng(position.coords.latitude, position.coords.longitude));
}, function() {
alert("Sorry, geolocation API failed to detect your location.");
});
});
document.getElementById("detect-btn").disabled = false;
}
}
</script>
<script src="//maps.googleapis.com/maps/api/js?v=3&sensor=false&key=AIzaSyBs1MVxS8kIxL9BXrxYE0lfvDP-iF_cmeA&callback=loadmap" defer></script>
</body>
</html>
答案 0 :(得分:2)
此错误通常发生,因为密钥参数是无效或此网站未被授权使用。在您的情况下,它很可能是您尝试重用其他资源中现有密钥的第二个原因。
基本上你有两个选择:
选项1
省略关键参数,因此请替换以下行:
<script src="//maps.googleapis.com/maps/api/js?v=3&sensor=false&key=AIzaSyBs1MVxS8kIxL9BXrxYE0lfvDP-iF_cmeA&callback=loadmap" defer></script>
带
<script src="//maps.googleapis.com/maps/api/js?v=3&sensor=false&callback=loadmap" defer></script>
选项2
Acquire a new key并用新的替换现有的。