English 中文(简体)
Google Map API v3 Infowinow 在鼠标翻转/单击多边形时不显示
原标题:Google Map API v3 Infowindow doesn t show up on mouseover/click on a polygon

我试图在信息窗口中显示动态数据, 每当用户在地图上多边形上徘徊时都会在信息窗口中显示动态数据。 调试显示数据和其他信息窗口/ polygon 设置都很好 。 我能够在鼠标上看到颜色变化, 只是信息窗口没有出现。 背后的原因是什么? 我在这里缺少什么?

statePolygon = new google.maps.Polygon({
    paths: stateBorderCoords,
    strokeColor:  #f33f00 , 
    strokeOpacity: 1, 
    strokeWeight: 1,
    fillColor:  #ff0000 , 
    fillOpacity: 0.2
});

statePolygon.pId = infoText; // Fetching from a JSON response
statePolygon.wPet = wPet;    // Fetching from a JSON response
statePolygon.infoWindow = new google.maps.InfoWindow();

google.maps.event.addListener(statePolygon,"mouseover",function(event){
    this.setOptions({fillColor: "#00FF00"});
    this.infoWindow.setPosition(event.latLng);
    this.infoWindow.setContent(this.wPet);
    this.infoWindow.open(map, this);
});

google.maps.event.addListener(statePolygon,"mouseout",function(){
    this.setOptions({fillColor: "#FF0000"});
    this.infoWindow.close();
});

google.maps.event.addListener(statePolygon,  click , function(){
    //createInfoWindow(this.pId);
});

statePolygon.setMap(map);
最佳回答

如果你把"这个"留到线上会怎么样?????

this.infoWindow.open(map, this);    

?????

过去几天来我一直在和类似的东西战斗, 刚刚发现我的代码对谷歌.maps.markers(如谷歌针)起作用, 但对于谷歌.maps没有作用. circles(我猜是谷歌.maps.polygons. Pollygons.)

我的猜测是:“infoWindow.open(map, object) ” 试图将InfoWindow锁定到 object 只对google.maps.markers.markers, 而不是圆圈、 多边形等有效。 似乎有效的是“ open(map) ”, 它不固定在任何东西上。 但是,InfoWindow 的位置必须被明确设置( 您已经这样做了 ) 。

<强>编辑:

对于我来说,这行不通(假设全球变数图)

var circle = { clickable: true,
               strokeColor: "darkred",
               strokeOpacity: 1,
               strokeWeight: 1,
               fillColor: "green",
               fillOpacity: 1,
               map: map,
               center: new google.maps.LatLng(55.95,-3.19),
               radius: 45
            };
var marker1 = new google.maps.Circle(circle);

infoWindow = new google.maps.InfoWindow();
infoWindow.setContent("Hello");
inoWindow.setPosition(new google.maps.LatLng(55.95,-3.19));
infoWindow.open(map,marker1);

但这样做确实:

var circle = { clickable: true,
               strokeColor: "darkred",
               strokeOpacity: 1,
               strokeWeight: 1,
               fillColor: "green",
               fillOpacity: 1,
               map: map,
               center: new google.maps.LatLng(55.95,-3.19),
               radius: 45
            };
var marker1 = new google.maps.Circle(circle);

infoWindow = new google.maps.InfoWindow();
infoWindow.setContent("Hello");
infoWindow.setPosition(new google.maps.LatLng(55.95,-3.19));
infoWindow.open(map);

< 强> 唯一的差别在最后一行。

在考虑上述职位后,在打开假定的空位后设定位置,将锚位倒置,从而使其出现。

问题回答

首先打开InfoWindow,然后设置位置:

http://jsfiddle.net/fuDfa/

google.maps.event.addListener(statePolygon,"mouseover",function(event){
    this.setOptions({fillColor: "#00FF00"});
    this.infoWindow.setContent(this.wPet);
    this.infoWindow.open(map);
    this.infoWindow.setPosition(event.latLng);
});

然而,我意识到,如果鼠标在InfoWindow上方移动(即使它位于多边形上方),它就会被算作鼠标在多边形外移动。因此,多边形会变成红色,而InfoWindow会关闭。但是,由于鼠标仍然在多边形内,该信息窗口会再次打开,造成闪烁。

我不知道怎么绕过这个话题(尝试了超时,但也不可靠 ) 。 我只是考虑把InfoWindow置于一个预设位置, 而不是事件.latLng。





相关问题
Why do I get "map.set_center is not a function"?

This code have been working until I updated last night. The code works perfectly on my localhost, but I get an error on my test server. The error message (from Firebug) is "map.set_center is not a ...

The state of GMaps v3

I m about to start a Google map based project and am wondering if the release version of GMaps v3 has most of the features that are available in v2, or if it would be best to stick with v2 for now. Is ...

Bouncy marker in Google Maps v3

In Google Maps API v2 we can set to marker an option bouncy:true. It adds to marker eye-candy ability - after dragging this marker, it is bouncing. Is it possible to do in API v3 ?

GUnload() a single Google Map

I ve got some Javascript/HTML code which displays a variable number of maps dependent on what the user selects. I ve worked how to dynamically create multiple maps on a page - that s well documented. ...

How to change Gmap markers color?

I ve a custom google map with different points: Markers[0] = new Array(new GMarker(new GLatLng(45.0, 9.0)), "Location1", "<strong>Address Line</strong><br/>Some information"); ...

热门标签