2012-06-13 145 views
1

我正在動態繪製一個多邊形並單擊以打開InfiWindow多邊形..我正在繪製多邊形succesfuly,但點擊Infowindow似乎並不像。不會給出一個錯誤只是看起來不!谷歌地圖V3繪製多邊形並打開InfoWındow

這是我所有的代碼;

 function ADD_EVENT_FOR_POLYLINE_AND_POLYGON() { 
      GLOBALS.PolyGonPath = new google.maps.MVCArray; 
      GLOBALS.PolyGon = new google.maps.Polygon({ 
       strokeWeight: 3, 
       fillColor: '#5555FF' 
      }); 
      GLOBALS.PolyGon.setMap(GLOBALS.Map); 
      google.maps.event.addListener(GLOBALS.Map, 'click', DRAW_POLYGON); 
     } 


    function DRAW_POLYGON(event) { 
     GLOBALS.PolyGonPath.insertAt(GLOBALS.PolyGonPath.length, event.latLng); 
     var marker = new google.maps.Marker({ 
      position: event.latLng, 
      map: GLOBALS.Map, 
      draggable: true 
     }); 
     GLOBALS.PolyMarkers.push(marker); 
     marker.setTitle("#" + GLOBALS.PolyGonPath.length); 
     google.maps.event.addListener(marker, 'click', function() { 
      marker.setMap(null); 
      for (var i = 0, I = GLOBALS.PolyMarkers.length; i < I && GLOBALS.PolyMarkers[i] != marker; ++i); 
      GLOBALS.PolyMarkers.splice(i, 1); 
      GLOBALS.PolyGonPath.removeAt(i); 
     }); 

     google.maps.event.addListener(marker, 'dragend', function() { 
      for (var i = 0, I = GLOBALS.PolyMarkers.length; i < I && GLOBALS.PolyMarkers[i] != marker; ++i); 
      GLOBALS.PolyGonPath.setAt(i, marker.getPosition()); 
     }); 

     **Here is I am adding a method to polygon for infowindow** 
     GLOBALS.PolyGon.setPaths(new google.maps.MVCArray([GLOBALS.PolyGonPath])); 
     google.maps.event.addListener(GLOBALS.PolyGon, 'click', SHOW_INFO); 
    }, 


    function SHOW_INFO (event) { 
     var infowin = new google.maps.InfoWindow(); 
     var vertices = GLOBALS.PolyGon.getPath(); 
     var contentString = "<b>Test</b><br />"; 

     for (var i = 0; i < vertices.length; i++) { 
      var xy = vertices.getAt(i); 
      contentString += "<br />" + "Coordinats: " + i + "<br />" + xy.lat() + "," + xy.lng(); 
     } 
     infowin = new google.maps.InfoWindow(); 
     infowin.setContent(contentString); 
     infowin.setPosition(event.latLng); 
     infowin.open(GLOBALS.Map, this); 
    } 

回答

0

嘗試更改的最後一行代碼在你SHOW_INFO功能:

infowin.setPosition(event.latLng); //Leave this line 
infowin.open(GLOBALS.Map, this); 

到:

infowin.setPosition(event.latLng); //This line stays the same 
infowin.open(GLOBALS.Map); 

的第二個參數infowin.open是一個可選MVCObject參數,與公共position屬性,用作錨點。在這種情況下,您不需要提供錨點,因爲您已經調用infowin.setPosition方法傳遞google.maps.LatLng。從google.maps.InfoWindowapi-doc描述的方法open的:

打開給定的地圖上的這個信息窗口。可選地,InfoWindow可以將 與錨點相關聯。在覈心API中,唯一的錨點是標記類 。然而,錨點可以是任何公開位置屬性的MVCObject和用於計算 pixelOffset(請參閱InfoWindowOptions)的可選anchorPoint。 anchorPoint是從錨的位置到InfoWindow的頂端的偏移量 。

+0

ı累infowin.open(GLOBALS.Map);但它是一樣的.. doesn似乎infowindow ..我點擊了三次地圖,並稱爲ADD_EVENT_FOR_POLYLINE_AND_POLYGON函數三次。並繪製三角形(多邊形),並在這裏添加一個事件(三次)到多邊形google.maps.event.addListener(GLOBALS.PolyGon,'click',SHOW_INFO);我認爲這是錯誤的。如何解決? – Mehmet

相關問題