如何为用户添加缩略图到 select 卫星视图?

How do I add a thumbnail for users to select a satellite view?

我对 mapbox 和 leaflet 很陌生。我正在尝试扩展 basic mapbox example here 以让用户单击一个小的缩略卫星图像,将他们带到卫星视图。我已经完成了 mapbox 和传单的示例,但看不到任何方法。可能吗? google 地图如何处理左下角的卫星视图:

https://www.google.com/maps/place/New+York,+NY/@40.6971494,-74.2598655,10z/data=!3m1!4b1!4m5!3m4!1s0x89c24fa5d33f083b:0xc80b8f06e177fe62!8m2!3d40.7127753!4d-74.0059728?hl=en-US

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>A simple map</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.mapbox.com/mapbox.js/v3.1.1/mapbox.js'></script>
<link href='https://api.mapbox.com/mapbox.js/v3.1.1/mapbox.css' rel='stylesheet' />
<style>
  body { margin:0; padding:0; }
  #map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<div id='map'></div>
<script>
L.mapbox.accessToken = 'pk.eyJ1IjoiYndhZGFtc29uIiwiYSI6ImNqajZhNm1idDFzMjIza3A2Y3ZmdDV6YWYifQ.9NhptR7a9D0hzWXR51y_9w';
var map = L.mapbox.map('map', 'mapbox.streets')
    .setView([40, -74.50], 9);
</script>
</body>
</html>

编辑:虽然这个例子是 mapbox js,但我真的不在乎它是 mapbox gl 还是 js。可以是。好的谢谢

您可以使用 mapbox static api 来预览卫星图像:

<img src="https://api.mapbox.com/styles/v1/mapbox/satellite-v9/static/-74.50000,40.00000,9.0,0,0/300x300?access_token=pk.eyJ1IjoiYndhZGFtc29uIiwiYSI6ImNqajZhNm1idDFzMjIza3A2Y3ZmdDV6YWYifQ.9NhptR7a9D0hzWXR51y_9w"/>

[https://www.mapbox.com/help/static-api-playground/]

更新:

您可以使用 mapbox/geo-viewport 库来计算预览的中心点和缩放,以及 render 事件来更新预览:

map.on('render', function() {
  setMapPreview()
})

function setMapPreview() {
  var bounds = map.getBounds().toArray()
  bounds = [bounds[0][0], bounds[0][1], bounds[1][0], bounds[1][1]]

  // The size of the desired map.
  var size = [100, 100];

  // Calculate a zoom level and centerpoint for this map.
  var vp = geoViewport.viewport(bounds, size, 0, 24, 512);

  // Construct a static map url
  // https://www.mapbox.com/developers/api/static/
  document.getElementById('preview').src =
    'https://api.mapbox.com/styles/v1/mapbox/satellite-v9/static/' +
    vp.center.join(',') + ',' + vp.zoom + ',0,0/' +
    size.join('x') + '?' +
    'attribution=false&logo=false&access_token=' + mapboxgl.accessToken;    
}

[https://jsfiddle.net/btv9ogpc/]

预览加事件点击没问题,旋转样式:

document.getElementById('preview').addEventListener('click', function () {
  map.setStyle('mapbox://styles/mapbox/satellite-v9')
})

[https://jsfiddle.net/xh74rb83]

听起来您可能会对某些 Leaflet plugins for Layer Switching Controls (mapbox.js 构建在 Leaflet 上感兴趣,因此它们应该是兼容的):

A tile driven basemaps control for Leaflet.

It allows you to create a user interface control for choosing the basemap used on the map, based on a tile from the underlying tile service.

See the example.

使用此插件,您只需指定一些常量图块坐标即可用作“预览”:

map.addControl(L.control.basemaps({
    basemaps: basemaps, // Array of Tile Layers.
    tileX: 0,  // tile X coordinate
    tileY: 0,  // tile Y coordinate
    tileZ: 1   // tile zoom level
}));

Leaflet control that displays base layers as small icons (demo).

对于此插件,即使 documentation uses different images 大小为 80x80 像素作为预览图标,您也可以很好地重复使用具有特定坐标的图块,插件会调整它们的大小以适合其图标:

var map = L.map('map').setView([48.86, 2.35], 5);

var OpenStreetMap_Mapnik = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  maxZoom: 19,
  attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
}).addTo(map);

var OpenTopoMap = L.tileLayer('https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png', {
  maxZoom: 17,
  attribution: 'Map data: &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>, <a href="http://viewfinderpanoramas.org">SRTM</a> | Map style: &copy; <a href="https://opentopomap.org">OpenTopoMap</a> (<a href="https://creativecommons.org/licenses/by-sa/3.0/">CC-BY-SA</a>)'
});

var layers = [{
  layer: OpenStreetMap_Mapnik,
  title: 'OSM Mapnik',
  icon: 'https://a.tile.openstreetmap.org/1/0/0.png'
}, {
  layer: OpenTopoMap,
  title: 'OSM Topo',
  icon: 'https://a.tile.opentopomap.org/1/0/0.png' // Re-use a tile
}];

L.control.iconLayers(layers).addTo(map);
html,
body,
#map {
  height: 100%;
  margin: 0;
}
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.1/dist/leaflet.css" integrity="sha512-Rksm5RenBEKSKFjgI3a41vrjkw4EVPlJ3+OiI65vTjIdo9brlAacEuKOiQ5OFh7cOI1bkDwLqdLw3Zg0cRJAAQ==" crossorigin="" />
<script src="https://unpkg.com/leaflet@1.3.1/dist/leaflet-src.js" integrity="sha512-IkGU/uDhB9u9F8k+2OsA6XXoowIhOuQL1NTgNZHY1nkURnqEGlDZq3GsfmdJdKFe1k1zOc6YU2K7qY+hF9AodA==" crossorigin=""></script>
<link rel="stylesheet" href="https://cdn.rawgit.com/ScanEx/Leaflet-IconLayers/ea9af769/dist/iconLayers.css" />
<script src="https://cdn.rawgit.com/ScanEx/Leaflet-IconLayers/ea9af769/dist/iconLayers.js"></script>

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

如果您愿意,也可以使用来自 mapbox static API 的图像,如 所示。