2017-10-09 134 views
0

我使用Leafletjs和D3来显示地图。LeafletJs只显示一个国家

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

单张和D3可能只显示英国。

+0

[图的点心/隐藏其余各地国leaflet.js(HTTPS的可能的复制。 COM /问题/ 25652189 /暗淡隐藏休息-的地图 - 环绕 - 国家与小叶-JS) – xmojmr

回答

2

这当然是可能的。

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

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

Prevent tiles outside of polygon from loading

的想法是使用TileLayer.BoundaryCanvas plugin,你可以指定一个以GeoJSON boundary选项可以隐藏一切,是不是GeoJSON的几何图形内。

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

现场演示用粗糙的英国形状://计算器:

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()); 
 
});
#map { 
 
    height: 500px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" /> 
 
<script src="https://unpkg.com/[email protected]/dist/leaflet-src.js"></script> 
 
<script src="https://cdn.rawgit.com/aparshin/leaflet-boundary-canvas/f00b4d35/src/BoundaryCanvas.js"></script> 
 

 
<div id="map"></div>

相关问题