在网页上使用Google Maps API,我可以使用键盘的上/下和左/右箭头键来平移地图。我希望允许用户像往常一样通过鼠标拖动/平移地图,但是想要在单击地图后从键盘箭头按下禁用平移。我怎样才能做到这一点?
对于后台,我使用网页上的箭头键来获取其他功能(上下移动HTML列表),并且当用户按下箭头键时不希望地图移动。
答案 0 :(得分:1)
您可以将<!DOCTYPE html>
<html>
<head>
<title>Simple Map</title>
<meta name="viewport" content="initial-scale=1.0">
<meta charset="utf-8">
<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;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
keyboardShortcuts: false,
gestureHandling: "greedy",
zoom: 8
});
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?callback=initMap&key=YOUR_KEY"
async defer></script>
</body>
</html>
中的keyboardShortcuts
选项设置为@Module
以禁用地图上的键盘操作
@Provides