gpt4 book ai didi

javascript - 使用ajax填充谷歌地图标记

转载 作者:行者123 更新时间:2023-11-29 14:53:38 24 4
gpt4 key购买 nike

我正在尝试使用 ajax 填充的 google.maps.LatLng 对象数组在 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>

我想我在这里遗漏了一些东西在将其分配给 position 之前,我是否应该将 google.maps.LatLng 对象的 latlng 数组解析为 LatLng

marker = new google.maps.Marker({
map: map,
position: latlng[i]
});

您的帮助将不胜感激,提前致谢,

最佳答案

我认为问题在于您没有考虑到 ajax 请求的异步性质。

您需要在 ajax 请求完成后构建标记。

将您的 for each 循环放在一个函数中,并在您成功的 ajax 回调的末尾 9 内调用它。

您还需要在加载 google map 后加载 ajax,否则您将无法创建 google latlng 对象,因为 google map 库可能尚未加载。

如果不重写这可能有效的所有内容..

$.ajaxSetup({
cache: false
});




var gMapsLoaded = false;
var latlng = [];
var returnValue;
var marker;
var xmlDoc;





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);


$.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()));
});

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);
});
}

},
failure: function (response) {
alert(response.d);
},
error: function (response) {
alert(response.d);
}
});



}

$(window).bind('gMapsLoaded', initialize);
window.loadGoogleMaps();
});

关于javascript - 使用ajax填充谷歌地图标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21555766/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com