我使用以下方式处理与Google地图关联的绘图工具:
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);
这很好用,但我想知道如何使用自定义按钮代替Polygon工具的默认控件。这样,当我按下按钮时,它处于活动状态,当我再次按下它时,它会将其取消激活。
这是我的自定义工具栏:
<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>
我试着通过api查看但我找不到任何东西。
供参考,这是我使用的网站: https://developers.google.com/maps/documentation/javascript/examples/drawing-tools
任何帮助都将不胜感激!
答案 0 :(得分:9)
有一种预定义的方法可以在Google Maps API中创建新的自定义控件和事件处理。
我在initialize()函数
中创建了新控件// 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..');
});
}
希望这会对你有帮助..看看我的小提琴 here