谷歌地图信息窗口 ajax 加载内容

Google map infowindow ajax load content

本文关键字:加载 ajax 窗口 信息 信息窗 谷歌地图      更新时间:2023-09-26

我无法从 ajax 调用加载信息窗口数据来自 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 文件时仍然返回未定义,例如

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

试试这个,使用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);      
        }
    });