如何为用户添加缩略图以选择卫星视图?

时间:2018-07-13 20:26:56

标签: javascript leaflet maps mapbox mapbox-gl-js

我对mapbox和传单非常陌生。我正在尝试扩展basic mapbox example here,以允许用户单击小的缩略图卫星图像,以将其带到卫星视图。我已经遍历了mapbox和Leaflet的示例,但看不到做到这一点的方法。可能吗?谷歌地图如何处理左下角的卫星视图:

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。可以是。好的,谢谢。

2 个答案:

答案 0 :(得分:5)

您可以使用mapbox静态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]

答案 1 :(得分:4)

一些Leaflet plugins for Layer Switching Controlsmapbox.js内置在Leaflet上,因此它们应该兼容)听起来像您一样感兴趣:

  

用于Leaflet的图块驱动的底图控件。

     

它允许您基于基础图块服务中的图块创建用于选择地图上使用的底图的用户界面控件。

     

请参见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
}));
  

将基本图层显示为小图标(demo)的传单控件。

animated capture of plugin in action

对于此插件,即使documentation使用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静态API中的图像,如stdob--'s answer中所示。