如何在Google地图中更改控件div中的按钮样式?
问题描述:
如何通过单击地图控件div上的按钮来获取元素和更改样式?我喜欢在地图上像在表格中的按钮一样具有相同的效果。如何在Google地图中更改控件div中的按钮样式?
代码:jsFiddle
更新:
在您的帮助我得到了预期的结果:jsFiddle2
答
我已经更新了你的gMap.select here,它的工作,并希望这是你想。
select: function (buttonId){
gMap.buttons.$line.className="unselected";
gMap.buttons.$placemark.className="unselected";
document.getElementById(buttonId).className="selected";
var divs=window.parent.frames[0].document.getElementsByTagName('div');
for(i=0;i<divs.length;i++)
{
if(divs[i].id.charAt(0)=="c") divs[i].className="unselected";
}
var btn='c_'+buttonId;
window.parent.frames[0].document.getElementById(btn).className="selected";
}
更新:Here is the updated fiddle.希望你想要这个。
更新被标记为//小提琴中的更新。
的你怎么可以添加自定义控制This is another example,它是在谷歌地图事件处理程序(使用JavaScript)
var map;
function initialize() {
map = new google.maps.Map(document.getElementById('map'));
map.setCenter(new google.maps.LatLng(48.77565,9.181802));
map.setZoom(12);
map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
var controlDiv = document.createElement('div');
var table=document.createElement('TABLE');
var tbdy=document.createElement('TBODY');
var tr=document.createElement('TR');
var btn1=document.createElement('input');
btn1.setAttribute("type", "button");
btn1.id="c_placemark_b";
btn1.setAttribute("value", "Button One");
var td1=document.createElement('TD');
td1.appendChild(btn1);
var btn2=document.createElement('input');
btn2.setAttribute("type", "button");
btn2.id="c_line_b";
btn2.setAttribute("value", "Button Two");
var td2=document.createElement('TD');
td2.appendChild(btn2);
tr.appendChild(td1);
tr.appendChild(td2);
tbdy.appendChild(tr);
table.appendChild(tbdy);
controlDiv.appendChild(table);
google.maps.event.addDomListener(btn1, 'click', function() {
//DoSomething
alert(this.id);
});
google.maps.event.addDomListener(btn2, 'click', function() {
//DoSomething
alert(this.id);
});
controlDiv.index = 1;
map.controls[google.maps.ControlPosition.TOP_RIGHT].push(controlDiv);
}
谢谢,这个解决方案是文字,但GMAP对象按钮的故意功能不可用,虽然我尝试修改。如何获取地图上创建的元素的id,将它分配给像gMap.buttons这样的正确按钮。$ line = document.getElementById(“line_b”);?主要的困难在于controlDiv中添加的新项目无法进行初始化,除非在您的代码中选择之后。抱歉切换接受。 – roza 2012-03-12 23:03:08
更新了小提琴,检查更新下面我的答案。 – 2012-03-13 00:04:56
我认为addDomListener是异步的,因为它工作到第二次点击。对于我的解决方案,我加入了你的两个解决方案并设置按钮,比如'gMap.buttons。$ line = btn2;'谢谢:) – roza 2012-03-13 08:27:29