2

How to get elements and change styles by clicking buttons on map control div? I like to have the same effect on map like in buttons in table.

Code: jsFiddle

Update:

With Your help I got expected result: jsFiddle2

roza
  • 585
  • 1
  • 9
  • 30

1 Answers1

2

I've updated your gMap.select here, it's working and hope this is what you want.

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";
}

Update : Here is the updated fiddle. Hope you want this.

Updates are marked with // Update in the fiddle.

This is another example of how you can add custom control and it's event handler in Google map (Using 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);
}
The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • Thanks, this solution is literal but the intentional functionality of the buttons in gMap object is unavailable, although I try to modify. How to get id of the elements created on map that it's assigned to right buttons like gMap.buttons.$line = document.getElementById("line_b");? The main difficulty is that the new items that are added in controlDiv can not get in initialize unless after select like in Your code. Sorry for toggle accepting. – roza Mar 12 '12 at 23:03
  • I think that addDomListener is asynchronous because it work until second time click. For my solution I join Your two solutions and set button like `gMap.buttons.$line = btn2;` Thanks:) – roza Mar 13 '12 at 08:27