2014-06-19 59 views
0

我試圖從標記點擊Google地圖而不是在新窗口中的標記。從地圖標記打開Goog​​le方向轉換爲div

目前我可以在新窗口中打開方向,但是我想在點擊標記時顯示的div中的iframe中打開它們。

我不能在iframe上使用靜態src url,因爲我在地圖上爲不同的標記使用了不同的URL。

任何幫助將是偉大的,這是我迄今爲止。

var depots = [ 
    ['Barnsley', 53.572664, -1.455800, 11, 'http://maps.google.com/?daddr=53.572664,-1.455800&directionsmode=driving'], 
    ['Birmingham', 52.359018, -1.938033, 10, 'http://maps.google.com/?daddr=52.359018,-1.938033&directionsmode=driving'], 
    ['Brentwood', 51.576452, 0.278843, 9, 'http://maps.google.com/?daddr=51.576452,0.278843&directionsmode=driving'], 
    ['Bristol', 51.501280, -2.366513, 8, 'http://maps.google.com/?daddr=51.501280,-2.366513&directionsmode=driving'], 
    ['Cambridge', 52.184396, -0.064012, 7, 'http://maps.google.com/?daddr=52.184396,-0.064012&directionsmode=driving'], 
    ['Edinburgh', 56.129890, -3.390296, 6, 'http://maps.google.com/?daddr=56.129890,-3.390296&directionsmode=driving'], 
    ['Gatwick', 51.152422, -0.216219, 5, 'http://maps.google.com/?daddr=51.152422,-0.216219&directionsmode=driving'], 
    ['Glasgow', 55.927129, -4.467189, 4, 'http://maps.google.com/?daddr=55.927129,-4.467189&directionsmode=driving'], 
    ['Heathrow', 51.508121, -0.387525, 3, 'http://maps.google.com/?daddr=51.508121,-0.387525&directionsmode=driving'], 
    ['Manchester', 53.220004, -2.414895, 2, 'http://maps.google.com/?daddr=53.220004,-2.414895&directionsmode=driving'], 
    ['Southampton', 50.959088, -1.345449, 1, 'http://maps.google.com/?daddr=50.959088,-1.345449&directionsmode=driving'], 


    ]; 
for (var i = 0; i < locations.length; i++) { 
    var depot = locations[i]; 
    var myLatLng = new google.maps.LatLng(depot[1], depot[2]); 
    var marker = new google.maps.Marker({ 
     position: myLatLng, 
     map: map, 
     icon: image, 
     shape: shape, 
     title: depot[0], 
     zIndex: depot[3], 
     url: depot[4] 
    }); 

    google.maps.event.addListener(marker, 'click', function() { 
     document.getElementById('direction').style.display = ""; 
     window.location.href = this.url; 
    }); 
} 

的HTML:

<div id="directionholder"> 
    <iframe id="directioniframe" frameborder="0" > </iframe> 
</div> 

我試圖做的this.url一個VAR和設定,在功能,但沒有運氣

var URLis = this.url; 
document.getElementById('directioniframe').src = " URLis "; 
+0

從「URLis」中刪除引號,並在最後一行代碼中使用URLis並嘗試。 –

+0

我得到一個:(在我的iframe中找不到文件或目錄 – MrHolroyd

+0

你有什麼「位置」?你可以在這裏發佈嗎? –

回答

0

你有沒有嘗試它拆一個函數?

google.maps.event.addListener(marker, 'click', direction); 
function direction(evt) 
{ 
    document.getElementById('directioniframe').src = this.url; 
} 
+0

剛剛試過,沒有jou ,我應該在哪裏放置URLis var?仍然在函數中? – MrHolroyd

+0

爲什麼你需要URL? – stefano

+0

你也可以設置一個標記的屬性,如「depotIndex」, 在子函數中讀取它,然後建立你的url depot array – stefano

0

這裏有一個簡單的例子,應該怎麼做:http://jsfiddle.net/lotusgodkk/x8dSP/3550/

我已經粘貼這應該是循環內的代碼。看看它。

JS:

var marker = new google.maps.Marker({ 
    position: myLatlng, 
    map: map, 
    title: 'Hello World!', 
    url:'http://jsfiddle.net' 
}); 
google.maps.event.addListener(marker, 'click', function() { 
    //document.getElementById('direction').style.display = ""; 
    var win = window.open(this.url, '_blank'); 
    win.focus(); 
}); 

關於你的評論中提到的錯誤,是指這樣的回答:Error 6 (net::ERR_FILE_NOT_FOUND): The file or directory could not be found

必須將錯誤由於協議的問題。另請注意,由於same origin policy,iframe不會加載。在這裏閱讀更多:http://javascript.info/tutorial/same-origin-security-policy

+0

所以,這不可能是由於到不同的域? – MrHolroyd

+0

是啊,最有可能的。 –

+0

如果y ou想閱讀更多關於它http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy –

相關問題