谷歌地图v3折线工具提示

时间:2011-02-25 02:09:37

标签: javascript google-maps google-maps-api-3

谷歌地图标记对象(google.maps.Marker)具有title属性,因此当用户将鼠标移到标记上时,会显示一个简单的工具提示。

折线上没有title属性(google.maps.Polyline)。有没有办法可以做到这一点/在V3中模拟这个? 我可以在V2 中执行此操作,但我找不到V3的示例。

4 个答案:

答案 0 :(得分:7)

我将{samshull的上述答案(正式投票!)与来自here的信息结合起来,使InfoWindow出现在用户光标位于该行的位置:

// Open the InfoWindow on mouseover:
google.maps.event.addListener(line, 'mouseover', function(e) {
   infoWindow.setPosition(e.latLng);
   infoWindow.setContent("You are at " + e.latLng);
   infoWindow.open(map);
});

// Close the InfoWindow on mouseout:
google.maps.event.addListener(line, 'mouseout', function() {
   infoWindow.close();
});

此处,line是您的PolyLine对象; map是您的Map对象;并且infoWindow是您的InfoWindow对象,我刚创建它:

var infoWindow = new google.maps.InfoWindow();

我也跟随this advice,为我的所有折线重复使用相同的InfoWindow对象,而不是为每一行创建一个新对象:

  

最佳做法:为获得最佳用户体验,只需一个信息窗口   应该在任何时候在地图上打开。多个信息窗口   地图显得杂乱无章。如果您一次只需要一个信息窗口,   您可以只创建一个InfoWindow对象并在不同的位置打开它   地图事件上的位置或标记,例如用户点击。如果你这样做   需要多个信息窗口,可以显示多个InfoWindow   对象同时出现。

请注意infoWindow.setContent()需要一个字符串。如果要在InfoWindow中显示数字,请在数字变量上调用toString()

我认为所有这些都是一个不完美的解决方法,直到Google地图希望有一天将title属性添加到PolylineOptions,就像他们已经为MarkerOptions做的那样。< / p>

答案 1 :(得分:6)

我不是100%这是唯一的方式,或者是最好的方式,但是在

上创建窗口的 方式

在Google地图V3中,您应该创建InfoWindow,然后使用myInfoWindow.setContent("Hello World!")

设置内容

为了让它在鼠标悬停时显示,您需要执行以下操作:


google.maps.event.addListener(myPolyline, 'mouseover', function() {
    myInfoWindow.open(mymap);
    // mymap represents the map you created using google.maps.Map
});

// assuming you want the InfoWindow to close on mouseout
google.maps.event.addListener(myPolyline, 'mouseout', function() {
    myInfoWindow.close();
});

答案 2 :(得分:3)

我在网上找到这个帮我做多边形的工具提示, 来自http://philmap.000space.com/gmap-api/poly-hov.html

var tooltip=function(){
var id = 'tt';
var top = 3;
var left = 3;
var maxw = 200;
var speed = 10;
var timer = 20;
var endalpha = 95;
var alpha = 0;
var tt,t,c,b,h;
var ie = document.all ? true : false;
return{
    show:function(v,w){         
        if(tt == null){             
            tt = document.createElement('div');
            tt.setAttribute('id',id);
            t = document.createElement('div');
            t.setAttribute('id',id + 'top');
            c = document.createElement('div');
            c.setAttribute('id',id + 'cont');
            b = document.createElement('div');
            b.setAttribute('id',id + 'bot');
            tt.appendChild(t);
            tt.appendChild(c);
            tt.appendChild(b);
            document.body.appendChild(tt);              
            tt.style.opacity = 0;
            tt.style.filter = 'alpha(opacity=0)';
            document.onmousemove = this.pos;                
        }
        tt.style.visibility = 'visible';
        tt.style.display = 'block';
        c.innerHTML = v;
        tt.style.width = w ? w + 'px' : 'auto';
        if(!w && ie){
            t.style.display = 'none';
            b.style.display = 'none';
            tt.style.width = tt.offsetWidth;
            t.style.display = 'block';
            b.style.display = 'block';
        }
        if(tt.offsetWidth > maxw){tt.style.width = maxw + 'px'}
        h = parseInt(tt.offsetHeight) + top;
        clearInterval(tt.timer);
        tt.timer = setInterval(function(){tooltip.fade(1)},timer);
    },
    pos:function(e){
        var u = ie ? event.clientY + document.documentElement.scrollTop : e.pageY;
        var l = ie ? event.clientX + document.documentElement.scrollLeft : e.pageX;
        tt.style.top = (u - h) + 'px';
        tt.style.left = (l + left) + 'px';
    },
    fade:function(d){
        var a = alpha;
        if((a != endalpha && d == 1) || (a != 0 && d == -1)){
            var i = speed;
            if(endalpha - a < speed && d == 1){
                i = endalpha - a;
            }else if(alpha < speed && d == -1){
                i = a;
            }
            alpha = a + (i * d);
            tt.style.opacity = alpha * .01;
            tt.style.filter = 'alpha(opacity=' + alpha + ')';
        }else{
            clearInterval(tt.timer);
            if(d == -1){tt.style.display = 'none'}
        }
    },
    hide:function(){
        clearInterval(tt.timer);
        tt.timer = setInterval(function(){tooltip.fade(-1)},timer);
    }
};
}();

此外,请参阅此SO讨论相同的主题: Tooltip over a Polygon in Google Maps 和, Google maps rectangle/polygon with title

答案 3 :(得分:2)

如果我没有弄错,我认为不可能设置工具提示,因为你提到PolygonOptions对象中没有title属性。但你可以制作一个div 看起来与工具提示完全相同,并在鼠标移动事件期间将其放在鼠标的尖端。我还试图找到一个解决方案,将此工具提示放在多边形中心的某处,但我认为这太麻烦了,这就是为什么我也认为谷歌人也没有实现它。 干杯