gpt4 book ai didi

javascript - 从 Json 数据异步加载 Google map 标记

转载 作者:行者123 更新时间:2023-11-29 19:35:46 27 4
gpt4 key购买 nike

目前我正在加载一张带有几百个标记的 map 。这仅适用于少数属性。但是,当我尝试加载许多标记时,页面在加载数据时卡住。

在我的初始化函数中,我正在加载 map 并创建标记。

var map;
var markers = [];

function initialize(id) {

// setup the map
map = new google.maps.Map(document.getElementById('map_canvas'), {
zoom: 13,
center: new google.maps.LatLng(lat, lon),
mapTypeId: google.maps.MapTypeId.ROADMAP
});

// wait for loaded and add markers
google.maps.event.addListener(map, 'idle', function () {
if (checkevent == false) {
createPropertyMarkers(); // add the markers
}
});
}
// end map

使用这个函数我添加了标记。

// create the property markers
function createPropertyMarkers() {
var bounds = map.getBounds();
//alert(bounds);

// loop through the json and get property data
for (var i = 0; i < markersdata.d.length; ++i) {
// set marker zindex
var zindex = 1;

// set the marker position
var latLng = new google.maps.LatLng(markersdata.d[i].lat,
markersdata.d[i].lon);

// set icon for property
var micon = '/images/home-2.png';
if (markersdata.d[i].count > 0) {
micon = '/images/home.png';
}

// set the main proerty marker to blue.
if (GetQueryStringParams('id') == markersdata.d[i].id) {
micon = '/images/homeBlue.png';
zindex = 10;
}

// drop the marker
var marker = new google.maps.Marker({
position: latLng,
map: map,
title: markersdata.d[i].address,
zIndex: zindex,
icon: micon
});
// set the marker
iwcontent = '<div id=\"content\" style=\"margin: 5px ; padding: 0; width: 220px; min-height: 250px;\">' +
'<h2 id=\"firstHeading\" class=\"firstHeading\" style=\"margin: -5px 0 1px 0; padding: 0;\">Property</h2>' +
'<div id=\"bodyContent\">' +
'<img src=\"/images/ajax-loader.gif\" alt=\"wait\" style=\"margin: 5px 0; padding: 0;\" /><br /><h3>Loading Info</h3><br /><br /><br /></div>' +
'<div id=\"propertyentityinfo\">' +
'</div></div>'
;

// add listener to open marker infowindow
attachListener(marker, iwcontent, markersdata.d[i].id);
// push markers to array
markers.push(marker);

//document.getElementById("testResults").innerHTML += i + " " + bounds.toString() + " - " + markersdata.d[i].lat + "," + markersdata.d[i].lon + " <br />";
}
// end loop
}

// load property markers
markersdata = getPropertyMarkers(GetQueryStringParams('id'));

在这里,我为将打开信息窗口并显示数据的图标添加了点击监听器。

// add the listener to the property markers
function attachListener(marker, content, id) {
google.maps.event.addListener(marker, "click", function () {
//infowindow.close();
checkevent = true;
infowindow.setContent(content);
map.setCenter(marker.getPosition());
infowindow.open(map, this);
setTimeout(function () {
loadPropertyInfo(id); // load infowindow data
checkevent = false;
}, 1000);
//setTimeout('checkevent = false', 3000);
});
}

问题来了。在我的函数中,从我的网络服务获取 json 数据。我正在使用 async: false 来让它工作。如果我把它拿出来,标记将不会加载。但是,当设置为 false 时,它​​还会导致网页等待数据进入。

如何修改我的代码以使其异步工作?

// get markers for loading
function getPropertyMarkers(propertyid) {
var mydata;

$.ajax({
url: "Service/MapData.asmx/getPropertyMarkers",
type: "POST",
data: "{'id': '" + propertyid + "'}",
async: false, <----------------- THE PROBLEM!
cache: true,
contentType: "application/json;",
dataType: "json",
success: function (data, textStatus, jqXHR) { //
mydata = data;
//initialize(propertyid);
},
error: function (xmlHttpRequest, textStatus, errorThrown) {
console.log(xmlHttpRequest.responseText);
console.log(textStatus);
console.log(errorThrown);
alert("Screen shot this error: " + xmlHttpRequest.toString() + " " + textStatus.toString() + " " + errorThrown.toString());
}
});
return mydata;
}

最佳答案

在回调中为您的 JSON 请求调用 createPropertyMarkers 函数(“成功”函数),我建议将返回的 json 传递给该函数而不是(或除此之外)使其成为全局函数.

// get markers for loading
function getPropertyMarkers(propertyid) {
var mydata;

$.ajax({
url: "Service/MapData.asmx/getPropertyMarkers",
type: "POST",
data: "{'id': '" + propertyid + "'}",
async: true,
cache: true,
contentType: "application/json;",
dataType: "json",
success: function (data, textStatus, jqXHR) { //
mydata = data;
createPropertyMarkers();
//initialize(propertyid);
},
error: function (xmlHttpRequest, textStatus, errorThrown) {
console.log(xmlHttpRequest.responseText);
console.log(textStatus);
console.log(errorThrown);
alert("Screen shot this error: " + xmlHttpRequest.toString() + " " + textStatus.toString() + " " + errorThrown.toString());
}
});
}

关于javascript - 从 Json 数据异步加载 Google map 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25209463/

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