0

我无法从 ajax 调用加载 infowwindow 数据来自 json 数组 这是循环代码...

function getResults(map){
    //remove all existing markers
    deleteOverlays();
    bounds = new google.maps.LatLngBounds();

    //set search center
    c = map.center;
    var cLat = c.lat().toFixed(6);
    var cLng = c.lng().toFixed(6);

    //get search radius
    var radius = $("#radius option:selected").val()

    $.ajax({
        url:"ajax/search_radius.php?lat="+cLat+"&lng="+cLng+"&radius="+radius,
        cache:false,
        async:true,
        type:"POST",
        dataType: "json",
        success:function(data){
            var l = data.markers.length;//alert(l);
            for (i = 0; i < l; i++) {
                var point = new google.maps.LatLng(data.markers[i][3], data.markers[i][4]);
                var pin = 'images/pins/' + data.markers[i][1] + '.png';
                //alert(pin);
                marker = new google.maps.Marker({
                    position: point,
                    panControl: false,
                    map:map,
                    icon:pin
                });
                bounds.extend(point);

                map.fitBounds(bounds);

                //add to valid array
                markersArray.push(marker);

                google.maps.event.addListener(marker, 'click', function() {
                    infowindow.close();
                    load_content(this,'5');
                });
            }
        }
    });
};

这是加载内容的功能

function load_content(marker, id){
  $.ajax({
    url: 'ajax/get_infowindow_content.php?id='+id,
    cache:true,
    dataType:"html",
    success: function(data){
      infowindow.setContent(data);
      infowindow.open(map, marker);
    }
  });
}

我遇到的问题是将标记的 id 传递给函数(上面显示即使在运行 php 文件时显式设置 ID 仍然返回未定义,例如

load_content(this,'5');

我也试过..(在其他变体中)

load_content(this, data.markers[i][0]); 

修改为以下仍然返回未定义的 id

google.maps.event.addListener(marker, 'click', function() {
   alert (id); //returns undefined
        return $.ajax({
            type: "POST",
            url: "ajax/get_infowindow_content.php?",
            data:{
                'id':id
            },
            success: function(data){
                infowindow.setContent(data);
                infowindow.setOptions({
                position: marker.getPosition()
            });
            infowindow.open(map, marker);      
        }
  });



                    //infowindow.close();
                    //load_content(this,'5');
                });
4

1 回答 1

1

试试这个,使用return

return $.ajax({
        type: "POST",
        url: "url",
        data:{
            'id':id
        },
        success: function(data){// function to be called if the request succeeds
            infowindow.setContent(data); //set the contents on infowindow
            infowindow.setOptions({
                position: marker.getPosition()
                });
            infowindow.open(map, marker);      
        }
    });
于 2013-09-27T07:13:46.903 回答