LeafsJs仅显示一个国家

ttm*_*tmt 2 javascript d3.js leaflet

我在D3中使用Leafletjs来显示地图。

我只想在地图上显示英国。

Leaflet和D3是否可以只显示英国。

ghy*_*ybs 5

当然有可能。

现在,解决方案取决于您是要使用D3绘制英国还是要从Tile Server中获取它。

在后一种情况下,有一种比xmojmr注释中的链接中提出的解决方案更新的解决方案。

请参见防止加载多边形外部的图块

这个想法是使用TileLayer.BoundaryCanvas插件,您可以指定一个GeoJSON boundary选项来隐藏不在该GeoJSON几何体中的所有内容。

BoundaryCanvas是Leaflet映射库的插件,用于绘制具有任意边界的平铺栅格图层。HTML5 Canvas用于渲染。

现场演示具有粗糙的英国形状:

var map = L.map("map");

$.getJSON('https://cdn.rawgit.com/johan/world.geo.json/34c96bba/countries/GBR.geo.json').then(function(geoJSON) {
  var osm = new L.TileLayer.BoundaryCanvas("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
    boundary: geoJSON,
    attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors, UK shape <a href="https://github.com/johan/world.geo.json">johan/word.geo.json</a>'
  });
  map.addLayer(osm);
  var ukLayer = L.geoJSON(geoJSON);
  map.fitBounds(ukLayer.getBounds());
});
Run Code Online (Sandbox Code Playgroud)
#map {
  height: 500px;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.2.0/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.2.0/dist/leaflet-src.js"></script>
<script src="https://cdn.rawgit.com/aparshin/leaflet-boundary-canvas/f00b4d35/src/BoundaryCanvas.js"></script>

<div id="map"></div>
Run Code Online (Sandbox Code Playgroud)