gpt4 book ai didi

javascript - 带有 Google map 绘图的自定义按钮

转载 作者:搜寻专家 更新时间:2023-11-01 05:04:50 25 4
gpt4 key购买 nike

我正在使用与 Google map 关联的绘图工具:

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);

这很好用,但我想知道如何使用自定义按钮而不是多边形工具的默认控件。这样当我按下按钮时它处于事件状态,当我再次按下它时它会停用。

这是我的自定义工具栏:

<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

如有任何帮助,我们将不胜感激!

最佳答案

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..');
});
}

希望这对你有帮助..看看我的 fiddle here

关于javascript - 带有 Google map 绘图的自定义按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28155703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com