gpt4 book ai didi

javascript - 如何切换 Google map 中多个 JSON 叠加层的可见性

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

我有多个 JSON 覆盖文件 - 区域 1 到 6。

我希望能够使用单选按钮菜单控制叠加层的可见性。

我尝试将每个覆盖层设置为变量并添加 if else 语句来切换可见性,但它不起作用。

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Map Template</title>
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#description {
font-family: Roboto;
font-size: 15px;
font-weight: 300;
}

#infowindow-content .title {
font-weight: bold;
}

#infowindow-content {
display: none;
}

#map #infowindow-content {
display: inline;
}

.pac-card {
margin: 10px 10px 0 0;
border-radius: 2px 0 0 2px;
box-sizing: border-box;
-moz-box-sizing: border-box;
outline: none;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
background-color: #fff;
font-family: Roboto;
}

#pac-container {
padding-bottom: 12px;
margin-right: 12px;
}

.pac-controls {
display: inline-block;
padding: 5px 11px;
}

#search {
position:absolute;

}

#pac-input {
width:400px;
margin: 10px;
padding: 0 11px 0 13px;
height: 40px;
background-color: #fff;
font-family: Roboto;
font-size: 17px;
font-weight: 300;


text-overflow: ellipsis;
width: 320px;
height: 40px;
border-width: 2px;
border: white;
box-shadow: none;
border-radius: 2px;
box-shadow: rgba(0, 0, 0, 0.3) 0px 1px 4px -1px;
font-family: Roboto, Arial, sans-serif;
font-weight: 400;
color: gray;
}

#pac-input:focus {
border-color: #4d90fe;
color: black;
}

#title {
color: #fff;
background-color: #4d90fe;
font-size: 25px;
font-weight: 500;
padding: 6px 12px;
}
#target {
width: 345px;
}
</style>
</head>
<body>
<div id="search">
<input id="pac-input" class="controls" type="search" placeholder="Search Any Address">
</div>
<form class="form">
<div class="switch-field">
<input type="radio" id="area1" name="switch-two" checked/>
<label for="area1">Area 1</label>

<input type="radio" id="area2" name="switch-two"/>
<label for="area2">Area 2</label>

<input type="radio" id="area3" name="switch-two"/>
<label for="area3">Area 3</label>

<input type="radio" id="area4" name="switch-two"/>
<label for="area4">Area 4</label>

<input type="radio" id="area5" name="switch-two"/>
<label for="area5">Area 5</label>

<input type="radio" id="area6" name="switch-two"/>
<label for="area6">Area 6</label>
</div>
</form>
<div id="map"></div>
<script>
// This example adds a search box to a map, using the Google Place Autocomplete
// feature. People can enter geographical searches. The search box will return a
// pick list containing a mix of places and predicted search terms.

// This example requires the Places library. Include the libraries=places
// parameter when you first load the API. For example:
// <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">


function initAutocomplete() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 5,
center: {lat: 52.656963, lng: -112.506664},
gestureHandling: 'greedy',
mapTypeControl: false
});



var area1 = new google.maps.Data();
var area2 = new google.maps.Data();
var area3 = new google.maps.Data();
var area4 = new google.maps.Data();
var area5 = new google.maps.Data();

area1.loadGeoJson('https://api.myjson.com/bins/myw18');
area2.loadGeoJson('https://api.myjson.com/bins/nkbn0');
area3.loadGeoJson('https://api.myjson.com/bins/cwnws');
area4.loadGeoJson('https://api.myjson.com/bins/106pnw');
area5.loadGeoJson('https://api.myjson.com/bins/7lwmk');




if (area1.checked) {

var area1 = new google.maps.Data();
var area2 = null;
var area3 = null;
var area4 = null;
var area5 = null;

} else if (area2.checked) {

var area1 = null;
var area2 = new google.maps.Data();
var area3 = null;
var area4 = null;
var area5 = null;

} else if (area3.checked) {

var area1 = null;
var area2 = null;
var area3 = new google.maps.Data();
var area4 = null;
var area5 = null;

} else if (area4.checked) {

var area1 = null;
var area2 = null;
var area3 = null;
var area4 = new google.maps.Data();
var area5 = null;

} else if (area5.checked) {

var area1 = null;
var area2 = null;
var area3 = null;
var area4 = null;
var area5 = new google.maps.Data();
}





map.data.setStyle(function(feature) {
return {
fillColor: feature.getProperty('COLOR'),
strokeWeight: 1,
strokeColor: 'black',
fillOpacity: 0.4,
strokeOpacity: 0.7,
zIndex: 0
};
});


// Infowindow
var infoWindow = new google.maps.InfoWindow({
zIndex: 2
});
map.data.addListener('click', function(event) {

map.data.revertStyle();
map.data.overrideStyle(event.feature, {strokeWeight: 2, strokeColor: 'black', zIndex: 1});

var CDNAME = event.feature.getProperty('CDNAME');
var COLOR = event.feature.getProperty('COLOR');

infoWindow.setPosition( event.latLng );
infoWindow.setOptions( {
pixelOffset: {width: 0, height: -3}
});

infoWindow.setContent(
"CDNAME: <b>" + CDNAME + "</b><br />" +
"COLOR: <b>" + COLOR + "</b>"
);
infoWindow.open(map);

});

map.data.addListener('clickout', function(event) {

map.data.revertStyle();
infoWindow.close();
});

map.data.addListener('mouseover', function(event) {

map.data.revertStyle();
map.data.overrideStyle(event.feature, {strokeWeight: 2, strokeColor: 'black', zIndex: 1});
});



// Create the search box and link it to the UI element.
var input = document.getElementById('pac-input');
var searchBox = new google.maps.places.SearchBox(input);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

// Bias the SearchBox results towards current map's viewport.
map.addListener('bounds_changed', function() {
searchBox.setBounds(map.getBounds());
});

var markers = [];
// Listen for the event fired when the user selects a prediction and retrieve
// more details for that place.
searchBox.addListener('places_changed', function() {
var places = searchBox.getPlaces();

if (places.length == 0) {
return;
}

// Clear out the old markers.
markers.forEach(function(marker) {
marker.setMap(null);
});
markers = [];

// For each place, get the icon, name and location.
var bounds = new google.maps.LatLngBounds();
places.forEach(function(place) {
if (!place.geometry) {
console.log("Returned place contains no geometry");
return;
}
var icon = {
url: 'https://www.adsforcarts.com/wp-content/uploads/2017/09/map-marker2.png',
size: new google.maps.Size(50, 75),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(25, 75)
};

// Create a marker for each place.
markers.push(new google.maps.Marker({
map: map,
icon: icon,
title: place.name,
position: place.geometry.location,
animation: google.maps.Animation.DROP
}));

if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
});
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&libraries=places&callback=initAutocomplete"
async defer></script>

</body>
</html>

加载 map 时,我只希望 Area1 可见,因为默认情况下会选中该单选选项。

当我选择另一个 radio (例如 Area4)时,我希望 Area1 变得不可见,而 Area4 变得可见。

最佳答案

当您希望在 map 上呈现 Data 对象时,请设置该对象的 map 属性。

来自 the documentation :

setMap
setMap(map)

Parameters:
map: Map
Return Value: None
Renders the features on the specified map. If map is set to null, the features will be removed from the map.

var area1 = new google.maps.Data();
var area2 = new google.maps.Data();
var area3 = new google.maps.Data();
var area4 = new google.maps.Data();
var area5 = new google.maps.Data();

area1.loadGeoJson('https://api.myjson.com/bins/myw18');
area2.loadGeoJson('https://api.myjson.com/bins/nkbn0');
area3.loadGeoJson('https://api.myjson.com/bins/cwnws');
area4.loadGeoJson('https://api.myjson.com/bins/106pnw');
area5.loadGeoJson('https://api.myjson.com/bins/7lwmk');

setArea();

function setArea() {
area1.setMap(document.getElementById('area1').checked ? map : null);
area2.setMap(document.getElementById('area2').checked ? map : null);
area3.setMap(document.getElementById('area3').checked ? map : null);
area4.setMap(document.getElementById('area4').checked ? map : null);
area5.setMap(document.getElementById('area5').checked ? map : null);
}

proof of concept fiddle

screenshot of resulting map

代码片段:

function initAutocomplete() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 5,
center: {
lat: 52.656963,
lng: -112.506664
},
gestureHandling: 'greedy',
mapTypeControl: false
});

var area1 = new google.maps.Data();
var area2 = new google.maps.Data();
var area3 = new google.maps.Data();
var area4 = new google.maps.Data();
var area5 = new google.maps.Data();

area1.loadGeoJson('https://api.myjson.com/bins/myw18');
area2.loadGeoJson('https://api.myjson.com/bins/nkbn0');
area3.loadGeoJson('https://api.myjson.com/bins/cwnws');
area4.loadGeoJson('https://api.myjson.com/bins/106pnw');
area5.loadGeoJson('https://api.myjson.com/bins/7lwmk');

setArea();

function setArea() {
area1.setMap(document.getElementById('area1').checked ? map : null);
area2.setMap(document.getElementById('area2').checked ? map : null);
area3.setMap(document.getElementById('area3').checked ? map : null);
area4.setMap(document.getElementById('area4').checked ? map : null);
area5.setMap(document.getElementById('area5').checked ? map : null);
}

google.maps.event.addDomListener(document.getElementById('area1'), 'click', setArea);
google.maps.event.addDomListener(document.getElementById('area2'), 'click', setArea);
google.maps.event.addDomListener(document.getElementById('area3'), 'click', setArea);
google.maps.event.addDomListener(document.getElementById('area4'), 'click', setArea);
google.maps.event.addDomListener(document.getElementById('area5'), 'click', setArea);

}
#map {
height: 90%;
}

html,
body {
height: 100%;
margin: 0;
padding: 0;
}
<form class="form">
<div class="switch-field">
<input type="radio" id="area1" name="switch-two" checked/>
<label for="area1">Area 1</label>

<input type="radio" id="area2" name="switch-two" />
<label for="area2">Area 2</label>

<input type="radio" id="area3" name="switch-two" />
<label for="area3">Area 3</label>

<input type="radio" id="area4" name="switch-two" />
<label for="area4">Area 4</label>

<input type="radio" id="area5" name="switch-two" />
<label for="area5">Area 5</label>
</div>
</form>
<div id="map"></div>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&libraries=places&callback=initAutocomplete" async defer></script>

关于javascript - 如何切换 Google map 中多个 JSON 叠加层的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55696527/

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