Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom button with Google Maps Drawing

I'm working with the drawing tool associated to Google Maps by using:

var drawingManager = new google.maps.drawing.DrawingManager({
        drawingMode: google.maps.drawing.OverlayType.Marker,
        drawingControl: true,
        drawingControlOptions: {
            position: google.maps.ControlPosition.Top_Center,
            drawingModes: [
                google.maps.drawing.OverlayType.POLYGON,
            ]
        },
    });
    drawingManager.setMap(map);

This works fine, but I was wondering how I could use a custom button instead of the default control for the Polygon tool. Such that when I press the button it is active and when I press it again it deactivates it.

This is my custom toolbar:

<div id="toolbar-container" class="toolbar-container">
    <div class="toolbar no-select">
        <div class="toolbar-button" id="clear-button">Clear Polygon</div>
        <div class="toolbar-button" id="polygon-button">Polygon</div>
    </div>
</div>

I tried looking through the api but I could not find anything.

For reference, this is the site I used: https://developers.google.com/maps/documentation/javascript/examples/drawing-tools

Any help with this would be appreciated!

like image 714
Ryan Sayles Avatar asked Dec 19 '22 07:12

Ryan Sayles


1 Answers

There is a predefined method to create new custom control and event handling in Google Maps API.

I have created the new control in initialize() function

// Create the DIV to hold the control and call the HomeControl() constructor
    var homeControlDiv = document.createElement('div');
    var homeControl = new HomeControl(homeControlDiv, map);

    homeControlDiv.index = 1;
    map.controls[google.maps.ControlPosition.TOP_CENTER].push(homeControlDiv);

//To set CSS and handling event for the control
    function HomeControl(controlDiv, map) {
        // Set CSS for the control border.
        var controlUI = document.createElement('div');
        controlUI.style.backgroundColor = 'blue';       
        controlUI.style.height = '23px';
        controlUI.style.marginTop = '5px';
        controlUI.style.marginLeft = '-9px';
        controlUI.style.paddingTop = '1px';
        controlUI.style.cursor = 'pointer';        
        controlUI.title = 'Your Custom function..';
        controlDiv.appendChild(controlUI);

        // Set CSS for the control interior.
        var controlText = document.createElement('div');
        controlText.style.padding = '11px';
        controlText.innerHTML = 'Custom Text';
        controlText.style.color = '#fff';    
        controlUI.appendChild(controlText);

        // Setup the click event listeners
        google.maps.event.addDomListener(controlUI, 'click', function () {
            alert('Your Custom function..');            
        });
    }

hope this will help you.. check out my fiddle here

like image 122
Hariharan G R Avatar answered Dec 31 '22 08:12

Hariharan G R