gpt4 book ai didi

javascript - 传单:同一页上的多个 map

转载 作者:行者123 更新时间:2023-11-29 15:11:58 24 4
gpt4 key购买 nike

我已经搜索过类似的问题,但没有找到适合我的案例的答案。我想使用 3 张传单 map ,每张都有不同的内容。出现两个问题:

  • 只显示第一个。
  • 比例和缩放控件仅在第三个中显示。

我附上了一个 jsfiddle,以防你能提供帮助。

const mapbox = L.tileLayer(mapboxUrl, {
id: 'mapbox.streets',
token: mapboxToken,
attribution: mapboxAttribution,
});

mapbox.addTo(mapOne);
mapbox.addTo(mapTwo);
mapbox.addTo(mapThree);

https://jsfiddle.net/eunderbridge/3dq9j1ur/10/

谢谢你:)

最佳答案

创建一个可重用的mapbox函数,每次都传递 map 实例:

const mapbox = (map) => {
return L.tileLayer(mapboxUrl, {
id: 'mapbox.streets',
token: mapboxToken,
attribution: mapboxAttribution,
}).addTo(map)
};

[mapOne, mapTwo, mapThree].forEach(mapInstance => mapbox(mapInstance));

const mapOne = L.map('mapOne', {
zoomControl: false,
maxZoom: 18,
minZoom: 6,
}).setView([40, -4], 6);

const mapTwo = L.map('mapTwo', {
zoomControl: false,
maxZoom: 18,
minZoom: 6,
}).setView([40, -4], 6);

const mapThree = L.map('mapThree', {
zoomControl: false,
maxZoom: 18,
minZoom: 6,
}).setView([40, -4], 6);

// Add a tile layer to the map (Mapbox Streets tile layer)
const mapboxToken = 'pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw';
const mapboxUrl = 'https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={token}';
const mapboxAttribution = [
'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors,',
'<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>,',
'Imagery © <a href="http://mapbox.com">Mapbox</a>',
].join(" ");

const mapbox = (map) => {
return L.tileLayer(mapboxUrl, {
id: 'mapbox.streets',
token: mapboxToken,
attribution: mapboxAttribution,
}).addTo(map)
};

[mapOne, mapTwo, mapThree].forEach(mapInstance => mapbox(mapInstance));


// Add a zoom control to the map
const zoomControl = new L.Control.Zoom({
position: 'topleft'
});
zoomControl.addTo(mapOne);
zoomControl.addTo(mapTwo);
zoomControl.addTo(mapThree);

// Add a scale
const scaleControl = L.control.scale({
maxWidth: 200,
metric: true,
imperial: false,
position: 'bottomright'
});
scaleControl.addTo(mapOne);
scaleControl.addTo(mapTwo);
scaleControl.addTo(mapThree);

// Add a fullscreen control to the map (plugin)
mapOne.addControl(new L.Control.Fullscreen());
mapTwo.addControl(new L.Control.Fullscreen());
mapThree.addControl(new L.Control.Fullscreen());
#mapOne,
#mapTwo,
#mapThree {
width: 80%;
height: 500px;
margin-top: 10px;
}
<!DOCTYPE html>
<html class="main-panel">

<head>
<meta charset="UTF-8">

<title>Turismo - Práctica </title>

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.1/dist/leaflet.css" />
<link href='https://api.mapbox.com/mapbox.js/plugins/leaflet-fullscreen/v1.0.1/leaflet.fullscreen.css' rel='stylesheet' />


</head>

<body>

<div class="cointainer-fluid" align="center">
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-12 col-xl-12">
<div id="mapOne">

</div>
</div>
</div>

<div class="row">
<div class="col-sm-12 col-md-12 col-lg-12 col-xl-12">
<div id="mapTwo">

</div>
</div>
</div>

<div class="row">
<div class="col-sm-12 col-md-12 col-lg-12 col-xl-12">
<div id="mapThree">

</div>
</div>
</div>


</div>




<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<script src="https://unpkg.com/leaflet@1.0.1/dist/leaflet.js"></script>
<script src='https://api.mapbox.com/mapbox.js/plugins/leaflet-fullscreen/v1.0.1/Leaflet.fullscreen.min.js'></script>


</body>

关于javascript - 传单:同一页上的多个 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53594814/

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