我想在获取ajax标记数据时在google地图上添加标记,并在该地图未加载后调用initMap
函数但是当我在initMap
函数之前添加警报时,会加载地图
这适用于并显示地图和标记:
$('#ddlLatLog').on("change", function() {
jQuery.getJSON('@Url.Action("GetLatLog", "MyTheme", new { area = "" })', {
id: $(this).find('option:selected').attr('Value')
},
function(jdata) {
markermap = jdata;
});
alert(markermap);
initMap(centermap, markermap);
});
不加载地图
$('#ddlLatLog').on("change", function() {
jQuery.getJSON('@Url.Action("GetLatLog", "MyTheme", new { area = "" })', {
id: $(this).find('option:selected').attr('Value')
},
function(jdata) {
markermap = jdata;
});
// alert(markermap);
initMap(centermap, markermap);
});
致电google api:
<script src="http://maps.google.com/maps/api/js?v=3.26&key=...callback=initMap" async defer></script>
为什么我需要提醒?
答案 0 :(得分:3)
没有alert
的地图未加载的原因是jQuery.getJSON
是async
来电。因此,当您放置alert
时,浏览器会有时间完成ajax
调用,并且在调用initMap
之前会返回响应。但在其他情况下,当您删除alert
时,它不会发生。为了克服这一点;您只需要在initMap(centermap, markermap);
的成功处理程序中移动jQuery.getJSON
,如下所示:
$('#ddlLatLog').on("change", function() {
jQuery.getJSON('@Url.Action("GetLatLog", "MyTheme", new { area = "" })', {
id: $(this).find('option:selected').attr('Value')
},
function(jdata) {
markermap = jdata;
initMap(centermap, markermap);
});
});