从Json数据异步加载Google Map Markers

时间:2014-08-08 17:56:27

标签: javascript json google-maps-api-3

目前我正在加载一个包含几百个标记的地图。这适用于少数几个属性。但是,当我尝试加载许多标记时,页面会在加载数据时冻结。

在我的初始化函数内部,我正在加载地图并创建标记。

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

在这里,我添加了点击监听器,用于打开infowindow并显示数据的图标。

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

现在问题就在这里。在我的函数中,从我的webservice获取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;
}

1 个答案:

答案 0 :(得分:3)

在JSON请求的回调中调用createPropertyMarkers函数(&#34;成功&#34;函数),我建议将返回的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());
    }
  });
}