javascript - Google Maps JS API 未显示 - 无错误
问题描述
这是我第一次使用 API,并且我搜索了过去提供给有相同问题的人的所有其他选项。我只是得到一个空白页,控制台也没有显示任何错误。这是我的代码:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0">
<meta charset="utf-8">
<style>
#map {
height: 100px;
width: 100px;
}
html, body {
height: 100px;
width: 100px;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id="map" style="width:100px; height:100px"></div>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=&callback=initMap" async defer></script>
<script>
var mLocations = null;
function initMap() {
var lLocations
var marker, i;
var infowindow = new google.maps.InfoWindow({});
$.getJSON("", function(pLocations) {
mLocations = pLocations;
for (i = 0; i < pLocations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(pLocations[i].lat, pLocations[i].lon),
map: map
});
google.maps.event.addListener(marker, 'Click', (function (marker, i) {
return function () {
infowindow.setContent(mLocations[i].info);
infowindow.open(map, marker);
}
}) (marker, i));
};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 20,
center: new google.maps.LatLng(lLocations[1].lat, lLocations[1].lon),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
});
}
</script>
</body>
</html>
对此的任何帮助将不胜感激,因为我现在没有想法。
解决方案
我在这里至少看到几个问题:
- 您在
map
初始化之前在 for 循环中使用该变量。我会在 getJSON 回调之外初始化地图; - 您没有设置中心选项;
- 如果 $.getJSON 不成功,您可能看不到地图 - 使用
console.log
或断点来查看是否正在评估您期望的所有行; - 你不导入 jQuery 来使用 $.getJSON
试试这个更新的initMap
功能:
function initMap() {
var marker, i;
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 11,
center: new google.maps.LatLng(0, 0);
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow({});
$.ajax({
url: "polarServer.asp?Screen=GetBlobScript&Script=LocationMap_GetMapData&SID=<%Context.WebSession.SessionIDcode%>",
dataType: 'json',
success: function(pLocations) {
if (pLocations instanceof Array) {
for (i = 0; i < pLocations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(pLocations[i].lat, pLocations[i].lon),
map: map
});
google.maps.event.addListener(marker, 'Click', function () {
infowindow.setContent(pLocations[i].info);
infowindow.open(map, marker);
});
}
if (pLocations.length > 0) {
map.setCenter(new google.maps.LatLng(pLocations[0].lat, pLocations[0].lon));
}
} else {
console.error("serve response is not an Array");
}
},
error: function(err){
console.error("Failed to get the data: ", err);
}
});
}
并确保在你的代码中嵌入 jQuery
推荐阅读
- node.js - 我一直在尝试连接以连接到 mongo compass 并查看我的用户和数据。但是,我的用户和数据的文件夹是空的
- javascript - 如何在 ReactJs 中使用 JsZip 获取文件的文件类型
- apache-kafka - KSQLDB rest api 确实创建具有定义工作的流?(Confluent 运算符)
- nginx - Kubernetes 上 worker_processes 的理想值
- typescript - 使用 ForkTsCheckerWebpackPlugin 的 Typescript 和 nuxt 内存泄漏
- snowflake-cloud-data-platform - Liquibase 快照因雪花而失败
- c - 使用 GHS MULTI 填充链接器脚本中的内存空白
- kotlin - lateinit 属性蓝牙套接字尚未初始化
- python - 使用 DataCatalog 保存数据
- java - 短信验证码请求失败:未知状态码:17093 null