传单:同一页上有多张地图

埃琳娜·费雷拉斯(Elena Ferreras)

我搜索了类似的问题,但没有找到适合我的情况的答案。我想使用3张传单地图,每张地图都有不同的内容。出现两个问题:

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

我附上了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/

谢谢 :)

kboul

创建一个可重用的mapbox函数,并每次传递地图实例:

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/[email protected]/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/[email protected]/dist/leaflet.js"></script>
    <script src='https://api.mapbox.com/mapbox.js/plugins/leaflet-fullscreen/v1.0.1/Leaflet.fullscreen.min.js'></script>


  </body>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章