2017-10-09 65 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>

相關問題