2011-08-12 300 views
0

任何人都可以幫忙。我有下面的代碼,但是當我點擊該標記它重定向我到同一頁:谷歌地圖標記url

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

<script type="text/javascript"> 
function initialize() { 
    var myOptions = { 
    zoom: 14, 
    center: new google.maps.LatLng(<?php echo $latitude; ?>, <?php echo $longitude; ?>), 
    mapTypeId: google.maps.MapTypeId.ROADMAP, 
    streetViewControl: true, 
    scrollwheel: false 
    } 

    var map = new google.maps.Map(document.getElementById('map_canvas'), myOptions); 

    for (var i = 0; i < locations.length; i++) { 
     var image = new google.maps.MarkerImage('images/greenmarkers/'+ i +'.png'); 
     var location = locations[i]; 
     var myLatLng = new google.maps.LatLng(location[1], location[2]); 
     var marker = new google.maps.Marker({ 
      position: myLatLng, 
      map: map, 
      icon: image, 
      title: location[0], 
      url: location[3] 
     }); 


google.maps.event.addListener(marker, 'click', function() { 
    window.location.href = marker.url; 
}); 
    } 
} 
</script> 

<script type="text/javascript"> 
var locations = [['test', 63.3740200000000, 19.6301320000000, 'http://www.test.com'],['test1', 63.3725155758185, 19.6264879703522, 'http://www.test1.com'],['test2', 63.3762550022764, 19.6305649280548, 'http://www.test2.com']]; 
</script> 
+0

貌似[共同作用,在 - 一個循環問題](http://stackoverflow.com/questions/3037598/how-to-get-around-the-jslint-error-dont-make-functions-within-a-loop)。請參閱http://stackoverflow.com/questions/6939983/creating-a-custom-google-map-with-multiple-markers-and-popup-windows-issues/6946209#6946209解決它的基本方法。 –

+0

謝謝馬特。我試過但沒有運氣。 – Stipe

+0

@Stipe試試我的答案... – ATOzTOA

回答

0
marker[i] = new google.maps.Marker({ 
      position: myLatLng, 
      map: map, 
      icon: image, 
      title: location[0], 
      url: location[3] 
     }); 


google.maps.event.addListener(marker[i], 'click', function() { 
    window.location.href = this.url; 
}); 
3

這裏是一個工作代碼:

function loadURL(marker) { 
    return function() { 
     window.location.href = marker.url; 
    } 
} 

function initialize() { 
    var myOptions = { 
    zoom: 5, 
    center: new google.maps.LatLng(50.00, 50.00), 
    mapTypeId: google.maps.MapTypeId.ROADMAP, 
    streetViewControl: true, 
    scrollwheel: false 
    } 

    locations=[["aaa",51.00, 51.00, "http://www.atoztoa.com"], ["bbb",52.00, 52.00, "http://www.google.com"]]; 

    var map = new google.maps.Map(document.getElementById('map_canvas'), myOptions); 

    for (var i = 0; i < locations.length; i++) { 
     var image = new google.maps.MarkerImage('roundedbutton1.png'); 
     var location = locations[i]; 
     var myLatLng = new google.maps.LatLng(location[1], location[2]); 
     var marker = new google.maps.Marker({ 
      position: myLatLng, 
      map: map, 
      icon: image, 
      title: location[0], 
      url: location[3] 
     }); 

     google.maps.event.addListener(marker, 'click', loadURL(marker)); 
    } 
} 
+0

謝謝。這對我有用! – sketchthat