【发布时间】:2014-02-28 14:46:54
【问题描述】:
我正在尝试使用 google.maps.LatLng 对象的 ajax 填充数组在 Google Map 上放置大约 120 个标记
这是我的脚本
<script type ="text/javascript">
$.ajaxSetup({
cache: false
});
var gMapsLoaded = false;
var latlng = [];
var returnValue;
var marker;
var xmlDoc;
$.ajax({
type: "POST",
url: "map.aspx/getLatLng",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
xmlDoc = $.parseXML(response.d);
$(xmlDoc).find("Table").each(function () {
latlng.push(new google.maps.LatLng($(this).find("lat").text(), $(this).find("lng").text()));
});
//alert(latlng.length.toString());
},
failure: function (response) {
alert(response.d);
},
error: function (response) {
alert(response.d);
}
});
window.gMapsCallback = function () {
gMapsLoaded = true;
$(window).trigger('gMapsLoaded');
}
window.loadGoogleMaps = function () {
if (gMapsLoaded) return window.gMapsCallback();
var script_tag = document.createElement('script');
script_tag.setAttribute("type", "text/javascript");
script_tag.setAttribute("src", "http://maps.google.com/maps/api/js?sensor=false&callback=gMapsCallback");
(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
}
$(document).ready(function () {
function initialize() {
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(24.678517, 46.702267),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map'), mapOptions);
for (var i = 0; i < latlng.length; i++) {
marker = new google.maps.Marker({
map: map,
position: latlng[i]
});
var infowindow = new google.maps.InfoWindow({
content: 'Location info:<br/>Country Name:<br/>LatLng:'
});
google.maps.event.addListener(marker, 'click', function () {
// Calling the open method of the infoWindow
infowindow.open(map, marker);
});
}
}
$(window).bind('gMapsLoaded', initialize);
window.loadGoogleMaps();
});
</script>
HTML
<div id ="map" style="width:850px; bottom:20px; height: 500px;">
</div>
我想我在这里遗漏了一些东西
我应该先将google.maps.LatLng 对象的latlng 数组解析为LatLng,然后再将其分配给position 吗?
marker = new google.maps.Marker({
map: map,
position: latlng[i]
});
您的帮助将不胜感激, 提前致谢,
【问题讨论】:
-
几个问题,ajax 调用真的触发了吗?为什么在ajax请求中请求JSON,然后解析成xml?
-
@Swires 是的,它触发即时通讯只是获取数据并使用数据填充
var latlng = [];,以便稍后在成功加载地图时使用。
标签: javascript ajax google-maps google-maps-api-3