2012-06-13 70 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

相关问题