gpt4 book ai didi

javascript - 根据要素属性使用外部按钮切换 Google Maps API 数据层要素

转载 作者:行者123 更新时间:2023-11-30 19:43:25 25 4
gpt4 key购买 nike

Google Maps API Data Layer - Dynamic Styling 的文档解释了如何向功能添加事件监听器,以便在单击该功能时,我们可以更改其属性。

如何使用 map 外部的按钮执行类似的操作?在 fiddle 示例中,如何通过单击“蓝色”按钮将 map 上具有“蓝色”属性的字母变为蓝色?

Fiddle example

<!DOCTYPE html>
<html>
<head>
<title>Data Layer: Dynamic Styling</title>
<meta name="viewport" content="initial-scale=1.0">
<meta charset="utf-8">
<style>#map {
height: 500px;
}
</style>
</head>
<body>
<button id="blue-button">blue</button>
<button id="red-button">red</button>
<button id="green-button">green</button>
<button id="yellow-button">yellow</button>
<div id="map"></div>
<script>

var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: {lat: -28, lng: 137}
});

// Load GeoJSON.
map.data.loadGeoJson(
'https://storage.googleapis.com/mapsdevsite/json/google.json');

// Color each letter gray. Change the color when the isColorful property
// is set to true.
map.data.setStyle(function(feature) {

var color = 'gray';

if (feature.getProperty('isColorful')) {
color = feature.getProperty('color');
console.log(color)
}

return ({
fillColor: color,
strokeColor: color,
strokeWeight: 2
});

});

// When the user clicks, set 'isColorful', changing the color of the letters.
map.data.addListener('click', function(event) {
event.feature.setProperty('isColorful', true);
});

}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB9BG3aO_hV9r8qaGkYmcE5eSx7c4K7be4&callback=initMap">
</script>
</body>
</html>

最佳答案

为您的按钮点击添加一个监听器。这可以通过不同的方式完成。其中之一是使用 Google map addDomListener

然后你必须遍历所有的特征并设置合适的样式,例如:

var map;

function initMap() {

map = new google.maps.Map(document.getElementById('map-canvas'), {
zoom: 4,
center: {
lat: -28,
lng: 137
}
});

// Load GeoJSON.
map.data.loadGeoJson(
'https://storage.googleapis.com/mapsdevsite/json/google.json');

// Color each letter gray. Change the color when the isColorful property
// is set to true.
map.data.setStyle(function(feature) {

var color = 'gray';

if (feature.getProperty('isColorful')) {
color = feature.getProperty('color');
console.log(color)
}

return ({
fillColor: color,
strokeColor: color,
strokeWeight: 2
});

});

// When the user clicks, set 'isColorful', changing the color of the letters.
map.data.addListener('click', function(event) {
event.feature.setProperty('isColorful', true);
});

google.maps.event.addDomListener(document.getElementById('blue-button'), 'click', function() {

map.data.setStyle(function(feature) {

if (feature.getProperty('color') == 'blue') {

return ({
fillColor: 'blue',
strokeColor: 'blue',
strokeWeight: 2
});
} else {

return ({
fillColor: 'grey',
fillOpacity: .5,
strokeColor: 'grey',
strokeWeight: 2
});
}
});
});

}
#map-canvas {
height: 160px;
}
<button id="blue-button">blue</button>
<div id="map-canvas"></div>

<!-- Replace the value of the key parameter with your own API key. -->
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap">
</script>

关于javascript - 根据要素属性使用外部按钮切换 Google Maps API 数据层要素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55167793/

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