简体   繁体   English

google api infoWindow在google maps api上显示1个标记

[英]google api infoWindow displaying under 1 marker on google maps api

I'm having a problem with the new google maps api when adding multiple markers and displaying infowindow for each marker, all my markers are showing but my main problem is that the infoWindow always shows at one place,I've looked everywere but can't seem to find the right solution, all my information that is to be displayed is coming from the database so i use the xmlhttp.responseText to get the info from another function which is getData. 当我添加多个标记并显示每个标记的infowindow时,我遇到了新的谷歌地图api的问题,所有我的标记都显示但我的主要问题是infoWindow总是显示在一个地方,我看起来每个人都可以'但是'似乎找到了正确的解决方案,我要显示的所有信息都来自数据库,因此我使用xmlhttp.responseText从另一个函数getData获取信息。

My code is as below 我的代码如下

  function showMap(data){     

 //Changing the json respose back to the javascript array
    var LongLat = eval( '(' + data + ')');

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


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

    latLng = new google.maps.LatLng(LongLat[i][0],LongLat[i][1]); 

    // Creating a marker and putting it on the map
    var marker = new google.maps.Marker({
    position:latLng,
    map: map,
    title: LongLat[i][0]
    });


    var infoWindow = new google.maps.InfoWindow();
    // Attaching a click event to the current marker

    google.maps.event.addListener(marker, "click", function(point){ 
        getData(this.position);
        xmlhttp.onreadystatechange=function()
        {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                infoWindow.setContent(xmlhttp.responseText);
                infoWindow.open(map,marker);
            }
        }               
        });
    }
 }

and my getData function is as follows 我的getData函数如下

function getData(d){

//separating the object to lantitude and longtitude p = (d.toString()); //将对象分为lantitude和longtitude p =(d.toString());

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

I think its better to reuse 1 infoWindow. 我认为最好重用1个infoWindow。 Have a look at this snippet which works for me: 看看这个适用于我的代码片段:

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

Your code, almost, is good :) Just few mistakes: 你的代码几乎是好的:)只是几个错误:

Put map, infoWindow as global 把map,infoWindow作为全局

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

Move marker creation to separate function. 将标记创建移动到单独的功能。 This will hide variables from changing 这将隐藏变量

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

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

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