2014-04-16 62 views
0

我有以下代碼,我想將外部鏈接添加到標記中,以便單擊鏈接的標記應在新窗口中打開。請幫助,以便我可以將站點鏈接到URL以在新窗口中打開。在地圖上的標記上設置外部鏈接

<html> 
<body> 
    <p>Chirag</p> 
    <div id="map_canvas" style="width:1000px; height:650px"></div> 
    <p>Blah</p> 

    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
    <script type="text/javascript"> 

    var map; 
    var markers = []; 

    initialize(); 

    function initialize() { 
     var myOptions = { 
     zoom: 14, 
     center: new google.maps.LatLng(23.2101344,72.6532201), 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
     } 
     map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 

     addMarker(new google.maps.LatLng(23.2101344,72.6532201), "Station1"); 
     addMarker(new google.maps.LatLng(23.230682, 72.635013), "Station2"); 
     addMarker(new google.maps.LatLng(23.217885, 72.642481), "Station3"); 

    } 

    function addMarker(latlng, myTitle) { 
     markers.push(new google.maps.Marker({ 
     position: latlng, 
     map: map, 
     title: myTitle, 
     icon: "http://maps.google.com/mapfiles/marker" + String.fromCharCode(markers.length + 65) + ".png" 
     }));  
    } 

    </script> 
</body> 

回答

0

觀察標記的點擊事件,並使用window.open打開所需的窗口:

//pass the desired URL as 3rd parameter 
function addMarker(latlng, myTitle,link) { 
    markers.push(new google.maps.Marker({ 
    position: latlng, 
    map: map, 
    title: myTitle, 
    icon: "http://maps.google.com/mapfiles/marker" + String.fromCharCode(markers.length + 65) + ".png" 
    })); 

    if(typeof link!=='undefined'){ 
    var winName='win'+markers.length; 
    google.maps.event.addListener(markers[markers.length-1],'click',function(){ 
     window.open(link,winName); 
    }) 

    }  
} 
相關問題