Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 谷歌api信息窗口显示在谷歌地图api上的1个标记下_Javascript_Google Maps_Google Maps Api 3 - Fatal编程技术网

Javascript 谷歌api信息窗口显示在谷歌地图api上的1个标记下

Javascript 谷歌api信息窗口显示在谷歌地图api上的1个标记下,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我在添加多个标记并为每个标记显示infowindow时遇到了新的google maps api问题,我所有的标记都会显示,但我的主要问题是infowindow总是显示在一个地方,我到处查看,但似乎找不到正确的解决方案,我要显示的所有信息都来自数据库,因此我使用xmlhttp.responseText从另一个函数getData获取信息 我的代码如下 //将对象分为长和长 p=(d.toString()) 我认为最好重用infoWindow。看看这段对我有用的代码: var infowindow

我在添加多个标记并为每个标记显示infowindow时遇到了新的google maps api问题,我所有的标记都会显示,但我的主要问题是infowindow总是显示在一个地方,我到处查看,但似乎找不到正确的解决方案,我要显示的所有信息都来自数据库,因此我使用xmlhttp.responseText从另一个函数getData获取信息

我的代码如下

//将对象分为长和长 p=(d.toString())


我认为最好重用infoWindow。看看这段对我有用的代码:

var infowindow = new google.maps.InfoWindow();


function addToMap() {

    for (var i = 0; i < marker_data.length; i++) {

        marker = new google.maps.Marker({
            position: new google.maps.LatLng(marker_data[i].lat, marker_data[i].lng),
            clickable: true,
            id:marker_data[i].id
        });

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

        marker.setMap(map);

        /* here we keep track of the markers that we have added to the page */
        markers_on_map.push(marker);
    }
}

function load_content(marker, id){
    $.ajax({
        url: '/map/getMarkerWindow/' + id,
        success: function(data){
            infowindow.setContent(data);
            infowindow.open(map, marker);
        }
    });
}
var infowindow=new google.maps.infowindow();
函数addToMap(){
对于(变量i=0;i
您的代码几乎是好的:)只有几个错误:

将地图、信息窗口设置为全局

    var map = null;
    var infoWindow = null;

    function initialize()
    {
        // Fill w/ sample data
        var LongLat = new Array();
        LongLat[0] = new Array(-26.5, 28.5);
        LongLat[1] = new Array(-26.0, 28.0);

        // Creating a new map
        map = new google.maps.Map(document.getElementById("map-canvas"), {
            center: new google.maps.LatLng(-26.1981, 28.0488),
            zoom: 5,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        });

        infoWindow = new google.maps.InfoWindow();

        for(var i = 0; i < LongLat.length; i++)
        {
            var latLng = new google.maps.LatLng(LongLat[i][0], LongLat[i][1]);

            AddMarkerToMap(latLng, i);

        }  // END FOR ( LatLng)
    }

你有没有试着在其他类似的问题中寻找答案?我有一种感觉,我已经多次看到这个问题了……谢谢,我只需要在事件侦听器中放入xmlhttp.onreadystatechange=function(),这样infowindow信息就可以是xml响应文本。。。
c = p.indexOf(',');
e = p.indexOf(')');
lat = p.substring(1,c);
lon = p.substring(c+1,e);

if(d == "results"){
    var htmlCode = t;
}

if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest(); 
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById('results').innerHTML = xmlhttp.responseText;
        }
    }

    if(xmlhttp.readyState==4 && xmlhttp.status==200){
        alert(xmlhttp.responseText);
        return xmlhttp.responseText;
    }
var html = xmlhttp.open("GET","mapmaker.php?lat="+lat+"&lon="+lon,true);
xmlhttp.send();
 }
var infowindow = new google.maps.InfoWindow();


function addToMap() {

    for (var i = 0; i < marker_data.length; i++) {

        marker = new google.maps.Marker({
            position: new google.maps.LatLng(marker_data[i].lat, marker_data[i].lng),
            clickable: true,
            id:marker_data[i].id
        });

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

        marker.setMap(map);

        /* here we keep track of the markers that we have added to the page */
        markers_on_map.push(marker);
    }
}

function load_content(marker, id){
    $.ajax({
        url: '/map/getMarkerWindow/' + id,
        success: function(data){
            infowindow.setContent(data);
            infowindow.open(map, marker);
        }
    });
}
    var map = null;
    var infoWindow = null;

    function initialize()
    {
        // Fill w/ sample data
        var LongLat = new Array();
        LongLat[0] = new Array(-26.5, 28.5);
        LongLat[1] = new Array(-26.0, 28.0);

        // Creating a new map
        map = new google.maps.Map(document.getElementById("map-canvas"), {
            center: new google.maps.LatLng(-26.1981, 28.0488),
            zoom: 5,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        });

        infoWindow = new google.maps.InfoWindow();

        for(var i = 0; i < LongLat.length; i++)
        {
            var latLng = new google.maps.LatLng(LongLat[i][0], LongLat[i][1]);

            AddMarkerToMap(latLng, i);

        }  // END FOR ( LatLng)
    }
    function AddMarkerToMap(latLng, i)
    {
        var marker = new google.maps.Marker({
            position:latLng,
            map:map
        });

        google.maps.event.addListener(marker, 'click', function()
        {
            infoWindow.setContent("Title: " + i);
            infoWindow.open(map, marker);
        });
    }