简体   繁体   English

Google Map Infowindow Ajax加载内容

[英]Google map infowindow ajax load content

I am having trouble getting an infowwindow loading from an ajax call The data comes from a json array This is the loop code... 我无法从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');
                });
            }
        }
    });
};

This is the function to load the content 这是加载内容的功能

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

The problem I am having is passing the id of the marker to the function (the above shows even setting an ID expliciltly still returns undefined when the php file is run eg 我遇到的问题是将标记的ID传递给函数(上面显示了即使在运行php文件时显式设置ID仍然返回undefined,例如

load_content(this,'5');

I have also tried..(amongst other variations) 我也试过了..(还有其他变化)

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

Amended to the following which still returns undefined for the id 修改为以下内容,该内容仍返回未定义的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');
                });

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM