簡體   English   中英

google api infoWindow在google maps api上顯示1個標記

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

當我添加多個標記並顯示每個標記的infowindow時,我遇到了新的谷歌地圖api的問題,所有我的標記都顯示但我的主要問題是infoWindow總是顯示在一個地方,我看起來每個人都可以'但是'似乎找到了正確的解決方案,我要顯示的所有信息都來自數據庫,因此我使用xmlhttp.responseText從另一個函數getData獲取信息。

我的代碼如下

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

我的getData函數如下

function getData(d){

//將對象分為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();
 }

我認為最好重用1個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);
        }
    });
}

你的代碼幾乎是好的:)只是幾個錯誤:

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

將標記創建移動到單獨的功能。 這將隱藏變量

    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