2015-01-05 82 views
-1

試圖讓我的標記指向另一個頁面。 複製了以前解決的案例的答案。仍然不起作用。 帶有標記的地圖出現OK。即使是標題在那裏,但鏈接不起作用。 保持消息「Uncaught ReferenceError:marker is not defined」。 我在做什麼錯在這裏?跑出想法。
有人可以幫我解決這個問題。 巨大的感謝;谷歌地圖自定義標記鏈接不工作

<script 
    src="https://maps.googleapis.com/maps/api/js?language=ko&key=AIzaSyA3WDZ1KfeUb_Q-SxtIRE2wZ4RBieuGl7s" 
    type="text/javascript"> 
</script> 
<script> 
function Goog2() { 
    var mapCanvas = document.getElementById('Google_map'); 
    var mapOptions = { 
     center: new google.maps.LatLng(45.80257,15.9371), 
     zoom: 10, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 
    var map = new google.maps.Map(mapCanvas, mapOptions); 

    var myLatLng0 = new google.maps.LatLng(45.80257,15.9371); 
    var myLatLng1 = new google.maps.LatLng(45.805455,15.983761); 
    var myLatLng2 = new google.maps.LatLng(45.738822, 16.068278); 
    var myLatLng3 = new google.maps.LatLng(45.803816, 15.993573); 

    var markers = []; 


    markers[0] = new google.maps.Marker({ 
     position: myLatLng0, 
     map: map, 
     url: '/cocohouse/location/CH_location_en.html', 
     icon: "/images/ariranglogoimage28_2.png", 
     title: 'Coco House', 
    }); 
    markers[1] = new google.maps.Marker({ 
     position: myLatLng1, 
     map: map, 
     icon: "/images/cocohouse/location/train26.png", 
     url:"/cocohouse/location/to_CH3_en.html", 
     title: 'Train Terminal', 

    }); 
    markers[2] = new google.maps.Marker({ 
     position: myLatLng2, 
     map: map, 
     icon: "/images/cocohouse/location/airport32.png", 
     url:"/cocohouse/location/to_CH1_en.html", 
     title: 'Airport', 
    }); 
    markers[3] = new google.maps.Marker({ 
     position: myLatLng3, 
     map: map, 
     icon: "/images/cocohouse/location/bus26.png", 
     url:"/cocohouse/location/to_CH2_en.html", 
     title: 'Bus Terminal', 
    }); 
    } 
    google.maps.event.addDomListener(window, 'load', Goog2); 

    for (i = 0; i < markers.lenght; i++) { 
     google.maps.event.addListener(markers[i], 'click', function() { 
      window.location.href = this.url; 
     }); 
    } 
</script> 
+0

「未捕獲的ReferenceError:標記沒有定義」:你有一個行號? – Thierry

+0

markers.lenght:只是一個錯字? markers.length – Thierry

+0

消息出現在.... markers.lenght .....線。當我把#4放在那裏時,錯誤消息向下移動到下一行。 –

回答

0

您在未定義上下文中使用「標記」。 '標記'在Goog2函數中聲明並在外部使用。

嘗試(未測試):

<script> 
    function Goog2() { 
    var mapCanvas = document.getElementById('Google_map'); 
    var mapOptions = { 
     center: new google.maps.LatLng(45.80257,15.9371), 
     zoom: 10, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 
    var map = new google.maps.Map(mapCanvas, mapOptions); 

    var myLatLng0 = new google.maps.LatLng(45.80257,15.9371); 
    var myLatLng1 = new google.maps.LatLng(45.805455,15.983761); 
    var myLatLng2 = new google.maps.LatLng(45.738822, 16.068278); 
    var myLatLng3 = new google.maps.LatLng(45.803816, 15.993573); 
    var markers = []; 
    markers[0] = new google.maps.Marker({ 
     position: myLatLng0, 
     map: map, 
     url: '/cocohouse/location/CH_location_en.html', 
     icon: "/images/ariranglogoimage28_2.png", 
     title: 'Coco House', 
    }); 
    markers[1] = new google.maps.Marker({ 
     position: myLatLng1, 
     map: map, 
     icon: "/images/cocohouse/location/train26.png", 
     url:"/cocohouse/location/to_CH3_en.html", 
     title: 'Train Terminal', 

    }); 
    markers[2] = new google.maps.Marker({ 
     position: myLatLng2, 
     map: map, 
     icon: "/images/cocohouse/location/airport32.png", 
     url:"/cocohouse/location/to_CH1_en.html", 
     title: 'Airport', 
    }); 
    markers[3] = new google.maps.Marker({ 
     position: myLatLng3, 
     map: map, 
     icon: "/images/cocohouse/location/bus26.png", 
     url:"/cocohouse/location/to_CH2_en.html", 
     title: 'Bus Terminal', 
    }); 

    for (i = 0; i < markers.length; i++) { 
     google.maps.event.addListener(markers[i], 'click', function() { 
      window.location.href = this.url; 
     }); 
    } 
    } 
    google.maps.event.addDomListener(window, 'load', Goog2); 

</script> 
+0

找到某個地方!錯誤信息消失了。 –

+0

但是,鏈接仍然不起作用。檢查.... –

+0

嘗試在單擊偵聽器中添加console.log(this.url)消息以查看偵聽器是否正常工作,並且URL是否正確。 – Thierry

0

markers.lenght;應該讀markers.length;

for (var i=0; i < markers.length; i++) { 
    google.maps.event.addListener(markers[i], 'click', function() { 
     window.location.href = this.url; 
    }); 
} 

markers需要在使用前進行定義。

您在窗口加載時調用了Goog2,但是在此事件之前和填充數組之前執行循環。

+0

工程..你們是國王的傢伙.... –

0

您正在標記存在之前添加點擊偵聽器。將該循環移到Goog2函數中。示例代碼片段(將標記更改爲公開可用的圖標,將URL更改爲任意公開的URL,修復了markers.length上的拼寫錯誤)。

function Goog2() { 
 
    var mapCanvas = document.getElementById('Google_map'); 
 
    var mapOptions = { 
 
    center: new google.maps.LatLng(45.80257, 15.9371), 
 
    zoom: 10, 
 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
 
    } 
 
    var map = new google.maps.Map(mapCanvas, mapOptions); 
 

 
    var myLatLng0 = new google.maps.LatLng(45.80257, 15.9371); 
 
    var myLatLng1 = new google.maps.LatLng(45.805455, 15.983761); 
 
    var myLatLng2 = new google.maps.LatLng(45.738822, 16.068278); 
 
    var myLatLng3 = new google.maps.LatLng(45.803816, 15.993573); 
 

 
    var markers = []; 
 

 

 
    markers[0] = new google.maps.Marker({ 
 
    position: myLatLng0, 
 
    map: map, 
 
    url: 'http://google.com', 
 
    icon: "http://maps.google.com/mapfiles/ms/micons/yellow.png", 
 
    title: 'Coco House', 
 
    }); 
 
    markers[1] = new google.maps.Marker({ 
 
    position: myLatLng1, 
 
    map: map, 
 
    icon: "http://maps.google.com/mapfiles/ms/micons/blue.png", 
 
    url: "http://yahoo.com", 
 
    title: 'Train Terminal', 
 

 
    }); 
 
    markers[2] = new google.maps.Marker({ 
 
    position: myLatLng2, 
 
    map: map, 
 
    icon: "http://maps.google.com/mapfiles/ms/micons/green.png", 
 
    url: "http://maps.google.com", 
 
    title: 'Airport', 
 
    }); 
 
    markers[3] = new google.maps.Marker({ 
 
    position: myLatLng3, 
 
    map: map, 
 
    icon: "http://maps.google.com/mapfiles/ms/micons/purple.png", 
 
    url: "http://www.cnn.com", 
 
    title: 'Bus Terminal', 
 
    }); 
 

 
    for (i = 0; i < markers.length; i++) { 
 
    google.maps.event.addListener(markers[i], 'click', function() { 
 
     window.location.href = this.url; 
 
    }); 
 
    } 
 
} 
 
google.maps.event.addDomListener(window, 'load', Goog2);
html, 
 
body, 
 
#Google_map { 
 
    height: 500px; 
 
    width: 500px; 
 
    margin: 0px; 
 
    padding: 0px 
 
}
<script src="https://maps.googleapis.com/maps/api/js"></script> 
 
<div id="Google_map" style="width:750px; height:450px; border: 2px solid #3872ac;"></div>

+0

它的作品!很好 –

+0

如果你的問題已經解決,請[接受其中一個答案](http://meta.stackoverflow.com/questions/5234/how-does-accepting-an-answer-work) – geocodezip