2016-03-02 39 views
1

我正試圖在地圖上顯示地圖。標記從數據庫中提取並放置在地圖上。但是,我想在點擊右側標記後顯示一個網站(放入數據庫)。我得到了打開不同頁面的代碼,但它只顯示數據庫表中最後一行的ID/URL。我怎樣才能讓它顯示每個位置的URL或ID而不僅僅是最後一個位置?谷歌地圖點擊標記從數據庫中打開網址

的代碼,我現在所擁有的:

function load() { 
    var map = new google.maps.Map(document.getElementById("map"), { 
     //defining the location 
    center: new google.maps.LatLng(45.466963, 9.188733), 
    //defining default map zoom 
    zoom: 14, 
    //disable scroll wheel function 
    scrollwheel: false, 
    //disable navigation control 
    navigationControl: false, 
    //disable map type control 
    mapTypeControl: false, 
    //disable scaling function 
    scaleControl: false, 
    //allowing the map to be dragable 
    draggable: true, 
    //disable user interface e.g. + and - buttons for zoom control 
    disableDefaultUI: true 

    }); 
    //creates window that contains defined information 
    infoWindow = new google.maps.InfoWindow; 


    //pulling the data from the file 
    downloadUrl("phpsqlajax_genxml2.php", function(data) { 

    //defining variables using data pulled over 


    var xml = data.responseXML; 
    var markers = xml.documentElement.getElementsByTagName("marker"); 
    for (var i = 1; i < markers.length; i++) { 
     var name = markers[i].getAttribute("name"); 
     var address = markers[i].getAttribute("address"); 
     var type = markers[i].getAttribute("type"); 
     var point = new google.maps.LatLng(
      parseFloat(markers[i].getAttribute("lat")), 
      parseFloat(markers[i].getAttribute("lng"))); 
     var html = "<b>" + name + "</b> <br/>" + address; 
     var idd = markers[i].getAttribute("identity"); 
     var icon = customIcons[type] || {}; 
     var marker = new google.maps.Marker({ 
      map: map, 
      position: point, 
      icon: icon.icon 
     }); 
     //when marker is clicked, it opens the URL of the location 
     bindInfoWindow(marker, map, infoWindow, html); 
       google.maps.event.addListener(marker, 'click', function() { 
         window.open(idd, '_blank'); 
         }); 
    } 
    }); 
} 

我曾嘗試幾個選項都無濟於事,如果我需要提供更多的信息,請不要這樣說,我在一個僵局

回答

0

您應該保存每個標記的「身份」值。您可以構建時,它的自定義數據添加到標記:

var marker = new google.maps.Marker({ 
     map: map, 
     position: point, 
     icon: icon.icon, 
     idd:idd 
    }); 

那麼聽衆應該使用這個新屬性:

google.maps.event.addListener(marker, 'click', function() { 
    window.open(this.idd, '_blank'); 
}); 
+0

這工作!非常感謝你! –

相關問題