gpt4 book ai didi

google-maps - 谷歌地图风格缩放控制

转载 作者:行者123 更新时间:2023-12-02 04:23:38 25 4
gpt4 key购买 nike

我正在使用 Google Maps V3 Javascript API,并且想要设置缩放控件的样式。尽管 Google 文档包含有关移动自定义控件的信息,但似乎没有任何用于设置自定义控件样式的信息。 https://developers.google.com/maps/documentation/javascript/controls#ControlModification

我想做的是能够设置现有缩放控件的样式。

有没有办法对现有控件进行样式设置?我更愿意这样做而不是创建新的自定义控件。

最佳答案

有一个选项可以使用谷歌的 map API 和自定义控制设置,这是我找到的代码片段:

  var map;
var chicago = new google.maps.LatLng(41.850033, -87.6500523);

/**
* The ZoomControl adds +/- button for the map
*
*/

function ZoomControl(controlDiv, map) {

// Creating divs & styles for custom zoom control
controlDiv.style.padding = '5px';

// Set CSS for the control wrapper
var controlWrapper = document.createElement('div');
controlWrapper.style.backgroundColor = 'white';
controlWrapper.style.borderStyle = 'solid';
controlWrapper.style.borderColor = 'gray';
controlWrapper.style.borderWidth = '1px';
controlWrapper.style.cursor = 'pointer';
controlWrapper.style.textAlign = 'center';
controlWrapper.style.width = '32px';
controlWrapper.style.height = '64px';
controlDiv.appendChild(controlWrapper);

// Set CSS for the zoomIn
var zoomInButton = document.createElement('div');
zoomInButton.style.width = '32px';
zoomInButton.style.height = '32px';
/* Change this to be the .png image you want to use */
zoomInButton.style.backgroundImage = 'url("http://placehold.it/32/00ff00")';
controlWrapper.appendChild(zoomInButton);

// Set CSS for the zoomOut
var zoomOutButton = document.createElement('div');
zoomOutButton.style.width = '32px';
zoomOutButton.style.height = '32px';
/* Change this to be the .png image you want to use */
zoomOutButton.style.backgroundImage = 'url("http://placehold.it/32/0000ff")';
controlWrapper.appendChild(zoomOutButton);

// Setup the click event listener - zoomIn
google.maps.event.addDomListener(zoomInButton, 'click', function() {
map.setZoom(map.getZoom() + 1);
});

// Setup the click event listener - zoomOut
google.maps.event.addDomListener(zoomOutButton, 'click', function() {
map.setZoom(map.getZoom() - 1);
});

}

function initialize() {
var mapDiv = document.getElementById('map-canvas');

var mapOptions = {
zoom: 12,
center: chicago,
/* Disabling default UI widgets */
disableDefaultUI: true
}

map = new google.maps.Map(mapDiv, mapOptions);

// Create the DIV to hold the control and call the ZoomControl() constructor
// passing in this DIV.
var zoomControlDiv = document.createElement('div');
var zoomControl = new ZoomControl(zoomControlDiv, map);

zoomControlDiv.index = 1;
map.controls[google.maps.ControlPosition.TOP_LEFT].push(zoomControlDiv);
}

initialize();

您可以在此处查看一个工作示例: http://jsfiddle.net/maunovaha/jptLfhc8/

关于google-maps - 谷歌地图风格缩放控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14970337/

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