1. <legend id='vsbMK'><style id='vsbMK'><dir id='vsbMK'><q id='vsbMK'></q></dir></style></legend>
    2. <small id='vsbMK'></small><noframes id='vsbMK'>

      <tfoot id='vsbMK'></tfoot>

          <bdo id='vsbMK'></bdo><ul id='vsbMK'></ul>

      1. <i id='vsbMK'><tr id='vsbMK'><dt id='vsbMK'><q id='vsbMK'><span id='vsbMK'><b id='vsbMK'><form id='vsbMK'><ins id='vsbMK'></ins><ul id='vsbMK'></ul><sub id='vsbMK'></sub></form><legend id='vsbMK'></legend><bdo id='vsbMK'><pre id='vsbMK'><center id='vsbMK'></center></pre></bdo></b><th id='vsbMK'></th></span></q></dt></tr></i><div id='vsbMK'><tfoot id='vsbMK'></tfoot><dl id='vsbMK'><fieldset id='vsbMK'></fieldset></dl></div>

        使用 Leaflet.js 和 OSM 时仅显示美国

        时间:2023-08-09
          <tbody id='LSYci'></tbody>

        <small id='LSYci'></small><noframes id='LSYci'>

            <legend id='LSYci'><style id='LSYci'><dir id='LSYci'><q id='LSYci'></q></dir></style></legend>

            <tfoot id='LSYci'></tfoot>

                <bdo id='LSYci'></bdo><ul id='LSYci'></ul>

                  <i id='LSYci'><tr id='LSYci'><dt id='LSYci'><q id='LSYci'><span id='LSYci'><b id='LSYci'><form id='LSYci'><ins id='LSYci'></ins><ul id='LSYci'></ul><sub id='LSYci'></sub></form><legend id='LSYci'></legend><bdo id='LSYci'><pre id='LSYci'><center id='LSYci'></center></pre></bdo></b><th id='LSYci'></th></span></q></dt></tr></i><div id='LSYci'><tfoot id='LSYci'></tfoot><dl id='LSYci'><fieldset id='LSYci'></fieldset></dl></div>
                  本文介绍了使用 Leaflet.js 和 OSM 时仅显示美国的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在使用 Leaflet.js 和 OSM 图块来创建地图,但我只希望看到美国大陆,而不是整个世界.这可能吗?

                  I'm using leaflet.js and OSM tiles to create a map, but I'd only like the continental United States to be viewable, not the entire world. Is that possible?

                  我正在像这样加载地图:

                  I'm loading the map like this:

                  var map = L.map('map').setView([39.82, -98.58], 5);
                  
                  L.tileLayer('http://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png', {
                      attribution: '...',
                      maxZoom: 18
                  }).addTo(map);
                  

                  推荐答案

                  这是可能的,而且很容易做到.首先,您需要美国大陆边界框(最外边缘)的坐标.你可以用谷歌搜索它们,我在这里找到它们:http://isithackday.com/geoplanet-explorer/index.php?woeid=24865672 您需要西南和东北坐标来创建边界对象:

                  That's possible and easy to do. First of you'll need the coordinates for the bounding box (the outermost edges) of the continental united states. You can just google them, i found them here: http://isithackday.com/geoplanet-explorer/index.php?woeid=24865672 You need the southwest and northeast coordinates to create a bounds object:

                  var maxBounds = L.latLngBounds(
                      L.latLng(5.499550, -167.276413), //Southwest
                      L.latLng(83.162102, -52.233040)  //Northeast
                  );
                  

                  或者您可以使用简写版本,即嵌套数组:

                  Or you can go for the shorthand version, a nested array:

                  var maxBounds = [
                      [5.499550, -167.276413], //Southwest
                      [83.162102, -52.233040]  //Northeast
                  ];
                  

                  现在您可以通过两种方式在地图上设置它们,初始化时,使用 maxBounds 选项和 L.MapfitBounds 方法>

                  Now you can set those on your map in two ways, upon initialization, using the maxBounds option and the fitBounds method of L.Map

                  L.map('map', {
                      'center': [0, 0],
                      'zoom': 0
                      'maxBounds': maxBounds
                  }).fitBounds(maxBounds);
                  

                  这是一个关于 Plunker 的工作示例:http://plnkr.co/edit/eEsxeh?p=预览

                  Here's a working example on Plunker: http://plnkr.co/edit/eEsxeh?p=preview

                  或者当您的地图已经初始化时,您可以使用 L.MapsetMaxBounds 方法和 fitBounds 方法.(假设您的地图已分配给变量 map):

                  Or when your map is already initialized you can use the setMaxBounds method and the fitBounds method of L.Map. (assuming your map is assigned to variable map):

                  map.setMaxBounds(maxBounds);
                  map.fitBounds(maxBounds);
                  

                  这是一个关于 Plunker 的工作示例:http://plnkr.co/edit/HJKk0O?p=预览

                  Here's a working example on Plunker: http://plnkr.co/edit/HJKk0O?p=preview

                  这篇关于使用 Leaflet.js 和 OSM 时仅显示美国的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何使用传单 js 在地图上精确放置 div 元素? 下一篇:如何找到最近的标记leaflet.js

                  相关文章

                  最新文章

                    1. <i id='8QNCK'><tr id='8QNCK'><dt id='8QNCK'><q id='8QNCK'><span id='8QNCK'><b id='8QNCK'><form id='8QNCK'><ins id='8QNCK'></ins><ul id='8QNCK'></ul><sub id='8QNCK'></sub></form><legend id='8QNCK'></legend><bdo id='8QNCK'><pre id='8QNCK'><center id='8QNCK'></center></pre></bdo></b><th id='8QNCK'></th></span></q></dt></tr></i><div id='8QNCK'><tfoot id='8QNCK'></tfoot><dl id='8QNCK'><fieldset id='8QNCK'></fieldset></dl></div>

                      <legend id='8QNCK'><style id='8QNCK'><dir id='8QNCK'><q id='8QNCK'></q></dir></style></legend>
                      • <bdo id='8QNCK'></bdo><ul id='8QNCK'></ul>

                      <small id='8QNCK'></small><noframes id='8QNCK'>

                    2. <tfoot id='8QNCK'></tfoot>