2014-01-11 86 views
7

我知道我可以做一个标记有一个提示,显示“东西”这样的显示工具提示:谷歌地图上的圆圈

marker = new google.maps.Marker({ 
     position: new google.maps.LatLng(lat,lon), 
     map: map, 
     draggable: true, 
     title:"SOMETHING", 
     icon: '/public/markers-map/male-2.png' 
    }); 

我想要做同样的一个圈,但冠军没有按没有工作。

new google.maps.Circle({ 
       center: new google.maps.LatLng(lat,lon), 
       radius: 20, 
       strokeColor: "blue", 
       strokeOpacity: 1, 
       title:"SOMETHING", 
       strokeWeight: 1, 
       fillColor: "blue", 
       fillOpacity: 1, 
       map: map 
      }); 

它打印圆形但不显示消息“SOMETHING”。

我该怎么办?有没有另一个属性来获得它?

在此先感谢。

回答

23

工具提示是通过本地title-DOM属性的属性创建的,但他的API不提供任何访问包含该圆的DOMElement的方法。

可能的解决方法可以是使用地图div的标题属性,而不是(设置onmouseover并将其取出onmouseout

 //circle is the google.maps.Circle-instance 
     google.maps.event.addListener(circle,'mouseover',function(){ 
      this.getMap().getDiv().setAttribute('title',this.get('title'));}); 

     google.maps.event.addListener(circle,'mouseout',function(){ 
      this.getMap().getDiv().removeAttribute('title');}); 
+0

这真棒!谢谢!!! :) – Himesh

+0

那个惊人的答案。谢谢:) – Sanket

3

你也可以用它代替HTML title属性信息窗口,为标题可能不会始终显示在鼠标上方。 InfoWindow看起来不错。

var infowindow = new google.maps.InfoWindow({}); 
var marker = new google.maps.Marker({ 
    map: map 
}); 

然后使用相同的鼠标悬停事件机制来显示信息窗口:

google.maps.event.addListener(circle, 'mouseover', function() { 
if (typeof this.title !== "undefined") { 
    marker.setPosition(this.getCenter()); // get circle's center 
    infowindow.setContent("<b>" + this.title + "</b>"); // set content 
    infowindow.open(map, marker); // open at marker's location 
    marker.setVisible(false); // hide the marker 
    } 
}); 

google.maps.event.addListener(circle, 'mouseout', function() { 
infowindow.close(); 
}); 
+0

也看看https://github.com/atmist/snazzy-info-window#examples –

1

此外,我们可以添加事件监听器google.maps.Circle实例指导。

代码示例:

//circle is the google.maps.Circle-instance 
circle.addListener('mouseover',function(){ 
    this.getMap().getDiv().setAttribute('title',this.get('title')); 
}); 

circle.addListener('mouseout',function(){ 
    this.getMap().getDiv().removeAttribute('title'); 
}); 

只是写了替代品!