谷歌地图infowindow ajax加载内容

时间:2013-09-27 07:04:20

标签: javascript jquery ajax google-maps infowindow

我无法从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传递给函数(上面显示甚至设置一个ID显然仍然在运行php文件时返回undefined,例如

load_content(this,'5');

我也试过......(除了其他变体)

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

修改为以下内容仍然为id

返回undefined
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');
                });

1 个答案:

答案 0 :(得分: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);      
        }
    });