2012-12-14 61 views
0

好吧,我已經花了一段時間在這上面,並且我偶然發現了很多教程和示例,並且得到了很多。多個多邊形,InfoWindow,setMap

我需要創建多個多邊形,每個多邊形都有一個單獨的InfoWindow。想想我已經知道了,但setMap有問題。這是多遠我有:

得到一個類型的錯誤

object #<object> has no method 'setMap' 

如何解決這個問題?

<script> 
var map; 
var infoWindow; 

function initialize() { 
    var myLatLng = new google.maps.LatLng(24.886436490787712, -70.2685546875); 
    var mapOptions = { 
    zoom: 5, 
    center: myLatLng, 
    mapTypeId: google.maps.MapTypeId.TERRAIN 
    }; 

map = new google.maps.Map(document.getElementById("map_canvas"), 
     mapOptions); 

    // Let's start with an empty object: 
var countries = {  
}; 

// Now let's add a county polygon: 
countries['UK'] = new google.maps.Polygon({ 
    paths: [ 
    new google.maps.LatLng(59.677361, -2.469846), 
    new google.maps.LatLng(59.299717, -6.314917), 
    new google.maps.LatLng(57.877247, -9.314917), 
    new google.maps.LatLng(54.428078, -11.638861), 
    new google.maps.LatLng(51.784554, -11.702241), 
    new google.maps.LatLng(50.185848, -10.054354), 
    new google.maps.LatLng(49.405380, -7.012100), 
    new google.maps.LatLng(49.090675, -3.272664), 
    new google.maps.LatLng(48.775970, -1.709283), 
    new google.maps.LatLng(49.757851, -2.089565), 
    new google.maps.LatLng(50.714554, 1.037195), 
    new google.maps.LatLng(51.482437, 2.304801), 
    new google.maps.LatLng(53.433609, 3.276632), 
    new google.maps.LatLng(55.863132, 3.445646) 
    // ... 
    ], 
    strokeColor: "#FF0000", 
    strokeOpacity: 0.8, 
    strokeWeight: 2, 
    fillColor: "#FF0000", 
    fillOpacity: 0.3 
}); 

// Next county: 
countries['FR'] = new google.maps.Polygon({ 
    paths: [ 
    // This is not real data: 
    new google.maps.LatLng(25.774252, -80.190262), 
    new google.maps.LatLng(18.466465, -66.118292), 
    new google.maps.LatLng(32.321384, -64.757370), 
    new google.maps.LatLng(25.774252, -80.190262) 
    // ... 
    ], 
    strokeColor: "#FF0000", 
    strokeOpacity: 0.8, 
    strokeWeight: 2, 
    fillColor: "#FF0000", 
    fillOpacity: 0.3 
}); 

// And so on... 

countries.setMap(map) 

; 
    // Add a listener for the click event 
    google.maps.event.addListener(UK, 'click', showInfoUK); 
    google.maps.event.addListener(FR, 'click', showInfoFR); 

    infowindow = new google.maps.InfoWindow(); 
} 

function showInfoUK(event) { 
    var contentString = "<b>UK</b><br />"; 
     contentString += "UK, Channel Islands, Ireland"; 



    // Replace our Info Window's content and position 
    infowindow.setContent(contentString); 
    infowindow.setPosition(event.latLng); 

    infowindow.open(map); 
} 
function showInfoFR(event) { 
    var contentString = "<b>FR</b><br />"; 
     contentString += "France, N,W"; 



    // Replace our Info Window's content and position 
    infowindow.setContent(contentString); 
    infowindow.setPosition(event.latLng); 

    infowindow.open(map); 
} 

</script> 
+0

和你的問題是什麼? – Marcelo

+0

該死!我收到setMap的錯誤 – m20b25

+0

javascript數組沒有.setMap()方法。 google.maps.Polygon()對象。另外,在javascript中沒有關聯數組,例如countries ['UK']。 – Marcelo

回答

0

JavaScript數組沒有.setMap()方法。 google.maps.Polygon()對象。此外,還有在JavaScript沒有關聯數組,比如國家[「英國」]

這可能會實現:

countries['UK'].setMap(map); 

,但它不會是正確的。

+0

謝謝,我早些時候嘗試過,但它得到了參考錯誤英國未定義。 – m20b25

+0

如果你在國家[英國],而不是國家['UK'],但是無論如何**沒有Javascript中的關聯數組**,我只能得到那個錯誤信息** – Marcelo

1

Javascript沒有引用像例如PHP這樣的鍵,所以國家['UK']是不可能設置的。你必須使用數字鍵。所以語法應該是這樣的:

countries[0] = new google.maps.Polygon({ ... }); 
countries[1] = new google.maps.Polygon({ ... }); 

我做你的榜樣工作:

http://jsfiddle.net/AcCzT/15/

可以微調,。這並不完美。這只是一個快速修復。 你可以從那裏繼續

+0

啊,我明白了,謝謝。但是,當我上傳它時,我得到一個main.js錯誤: 對象沒有方法'加載' – m20b25

+0

你包括谷歌地圖api js正確嗎? main.js是Google使用的主要文件。你的初始化代碼是什麼? –

+0

嗨渡船,我使用你的代碼從小提琴創建300多個多邊形(英國縣界)。然而,我不確定如何給彈出窗口提供他們需要的信息,我現在不能讓它彈出。 使用此處的代碼對每個新信息窗口和匹配函數進行硬編碼。我是否需要將該函數包含在我的foreach循環中並動態創建它? – Chris

0

使用ajax更簡單,更簡單。 例如,

downloadUrl("url", function(data) { 
    for (var i = 0; i < polygons.length; i++) { 
     ........ 
     var decodedPath = ........ 
     polygon.setMap(map); 
     ........ 
    } 
}); 
+0

不要發佈直接鏈接....在您的答案...或expalin中寫一些代碼.. – bipen

+0

謝謝你的例子,但沒有太多的用處,因爲我看不到你的代碼能夠看到如何你已經做到了。 – m20b25

0

有在你的代碼的兩個問題:

  1. 什麼馬塞洛說,多邊形添加到地圖上,使用:countries["UK"].setMap(map);
  2. 要添加點擊監聽器,你需要引用的多邊形:google.maps.event.addListener(countries["UK"], 'click', showInfoUK);

working example