gpt4 book ai didi

javascript - 使用传单 maxbounds 设置边界但不起作用

转载 作者:行者123 更新时间:2023-11-30 14:10:41 26 4
gpt4 key购买 nike

您好,我尝试在传单 map 中设置 maxbaounds,但它不起作用。我的第一个问题是 maxbounds 停止平移到我设置的边界之外?我按照这个例子,但我的代码有问题

Leaflet maxBounds - bounds do not work

https://docs.mapbox.com/mapbox.js/example/v1.0.0/maxbounds/

我的代码

<html>
<head>
<title>A Leaflet map!</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.4.0/dist/leaflet.css" />
<script src="https://maps.api.2gis.ru/2.0/loader.js?pkg=full"></script>
<style>
#map{ height: 100% }
</style>
</head>
<body>

<div id="map"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://unpkg.com/leaflet@1.4.0/dist/leaflet.js"></script>
<script>

var southWest = L.latLng(34.072711,31.758391),
northEast = L.latLng(36.113055, 35.124228),
bounds = L.latLngBounds(southWest, northEast);

var counties = $.ajax({
url: "https://gist.githubusercontent.com/vassilaros/3791204ca226d5b236b4cd3106ef23cf/raw/PicnicSites.geojson",
dataType: "json",
success: console.log("County data successfully loaded."),
error: function(xhr) {
alert(xhr.statusText)
}
})
$.when(counties).done(function() {
var map = L.map('map')
.setView([35.126411,33.429859], 9);

//tiles - http://{s}.basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}.png
var basemap = L.tileLayer('http://tile1.maps.2gis.com/tiles?x={x}&y={y}&z={z}', {
attribution: '&copy; <a href="http://info.2gis.com/">2GIS</a> &copy; <a href="https://www.opencartography.com">Open Cartography</a>',
subdomains: 'abcd',
maxBounds: bounds,
maxZoom: 19,
minZoom: 9
}).addTo(map);

// Add requested external GeoJSON to map
var kyCounties = L.geoJSON(counties.responseJSON).addTo(map);
var scale = L.control.scale()
scale.addTo(map)
});
</script>
</body>
</html>

最佳答案

L.map 上使用 maxBounds 设置边界限制。

var map = L.map('map')
.setView([35.126411,33.429859], 9)
.setMaxBounds(bounds);

另外 - 不要混淆 mapbox.jsleaflet.js - 它们是不同的。

关于javascript - 使用传单 maxbounds 设置边界但不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54513990/

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