0

我有以下代码,我想在这里调用初始化函数;google.maps.event.addDomListener(window, 'load', 初始化);

但是我收到了这个错误;错误:未定义初始化。

这里有什么问题?

$(function CheckinMap() {
        $.ajax({
            type: "GET",
            url: "content/home/index.cs.asp?Process=ViewCheckinMap",
            success:     function initialize(data) {
                var center = new google.maps.LatLng(48.404840395764175, 2.6845264434814453);

                var map = new google.maps.Map(document.getElementById('map'), {
                  zoom: 2,
                  center: center,
                  mapTypeId: google.maps.MapTypeId.ROADMAP,
                  maxZoom: 4
                });

                var markers = [];
                for (var i = 0; i < data.users.length; i++) {
                  var location = data.users[i];
                  var latLng = new google.maps.LatLng(location.latitude,
                      location.longitude);
                  var marker = new google.maps.Marker({
                    position: latLng
                  });
                  markers.push(marker);
                }
                var markerCluster = new MarkerClusterer(map, markers);
            },
            error: function (data) {
                $("#checkinmap").append(data);
            }
        });
    });
google.maps.event.addDomListener(window, 'load', initialize);
4

2 回答 2

1

如果你想为jQuery方法和第三个参数使用一个函数,那么你必须在定义它时将它存储在某个地方,而不是直接将它传递给.successaddDomListenersuccess

某个地方也必须在您调用 toajax和 to的范围内addDomLister

移动function initialize(data),使其出现在您当前的第 1 行之前。

然后说success: initialize

于 2012-06-04T10:25:42.317 回答
0

您正在定义intialize内部CheckinMap范围,然后尝试在代码的最后一行中在该范围之外使用它。如果您确实需要在两个地方都有它,请在外部定义它,然后在success属性值中引用。

function initialize(data) {
    var center = new google.maps.LatLng(48.404840395764175, 2.6845264434814453);

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 2,
      center: center,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      maxZoom: 4
    });

    var markers = [];
    for (var i = 0; i < data.users.length; i++) {
      var location = data.users[i];
      var latLng = new google.maps.LatLng(location.latitude,
          location.longitude);
      var marker = new google.maps.Marker({
        position: latLng
      });
      markers.push(marker);
    }
    var markerCluster = new MarkerClusterer(map, markers);
}

$(function CheckinMap() {
        $.ajax({
            type: "GET",
            url: "content/home/index.cs.asp?Process=ViewCheckinMap",
            success: initialize,
            error: function (data) {
                $("#checkinmap").append(data);
            }
        });
    });
google.maps.event.addDomListener(window, 'load', initialize);
于 2012-06-04T10:26:33.600 回答