我正在使用MapboxGLJSv0.14.2,我已经在文档中搜索了很多和低的内容,而且对此了解甚少。
如果您使用标准的JS,那么使用它们提供的示例(https://www.mapbox.com/mapbox.js/example/v1.0.0/fit-map-to-markers/)可以很清楚地“将映射到标记”;但是,使用GL时的设置是非常不同的。GL有getBounds() (https://www.mapbox.com/mapbox-gl-js/api/#Map.getBounds),但是由于没有命名层,比如标准的JS,所以我很难找到如何使用getBounds()。
我找到了这个(Mapbox GL JS API集界限),但肯定不是正确的答案?
这是我的大部分设置;不包括JSON设置和其他选项。
mapboxgl.accessToken = '<myaccesstoken>';
var markers = <?php echo $programme_json; ?>;
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/richgc/cikyo5bse00nqb0lxebkfn2bm',
center: [-1.470085, 53.381129],
zoom: 15
});
map.on('style.load', function() {
map.addSource('markers', {
'type': 'geojson',
'data': markers
});
map.addLayer({
"id": "markers",
"interactive": true,
"type": "symbol",
"source": "markers",
"layout": {
"icon-image": "venue-map-icon-blue",
'icon-size': 0.5,
"icon-allow-overlap": true
}
});
map.scrollZoom.disable();
});我尝试了以下几点:
alert(map.getBounds()); // LngLatBounds(LngLat(-1.4855345239256508, 53.37642500812015), LngLat(-1.4546354760740883, 53.38583247227842))
var bounds = [[-1.4855345239256508, 53.37642500812015],[-1.4546354760740883, 53.38583247227842]]
map.fitBounds(bounds);因此,我知道如何fitBounds,但我不知道如何让他们map.getBounds()似乎只是返回设定的中心位置lng/lat。
标记JSON:
var markers = {"type":"FeatureCollection","features":[{"type":"Feature","properties":{"title":"Site Gallery","url":"\/Freelance\/art-sheffield-2016\/programme\/site-gallery\/","summary":"Duis arcu tortor, suscipit eget, imperdiet nec, imperdiet iaculis, ipsum. Donec id justo. Aenean tellus metus, bibendum sed, posuere ac, mattis non, nunc. Suspendisse feugiat. Etiam rhoncus.","image":"\/Freelance\/art-sheffield-2016\/site\/assets\/files\/1032\/site_gallery.jpg","marker-symbol":"venue-map-icon-blue","colour":"blue"},"geometry":{"type":"Point","coordinates":["-1.466439","53.376842"]}},{"type":"Feature","properties":{"title":"Moore Street Substation","url":"\/Freelance\/art-sheffield-2016\/programme\/moore-street-substation\/","summary":"","image":null,"marker-symbol":"venue-map-icon-green","colour":"green"},"geometry":{"type":"Point","coordinates":["-1.477881","53.374798"]}},{"type":"Feature","properties":{"title":"S1 Artspace","url":"\/Freelance\/art-sheffield-2016\/programme\/s1-artspace\/","summary":"","image":null,"marker-symbol":"venue-map-icon-red","colour":"red"},"geometry":{"type":"Point","coordinates":["-1.459620","53.380562"]}}]};发布于 2016-03-01 05:09:55
如果要将映射与标记相匹配,则可以创建包含所有标记的边界。
var bounds = new mapboxgl.LngLatBounds();
markers.features.forEach(function(feature) {
bounds.extend(feature.geometry.coordinates);
});
map.fitBounds(bounds);发布于 2016-09-01 12:07:14
要获得适用于所有GeoJSON对象的解决方案,而不仅仅是一组标记,请查看Mapbox的Turf.js。
这段代码对我很有帮助:https://bl.ocks.org/danswick/83a8ddff7fb9193176a975a02a896792
但是,为了重复这些基本知识,以防链接消失:
var bounds = turf.bbox(markers);
map.fitBounds(bounds, {padding: 20});在链接代码中提到的extent方法已经被反对,以支持bbox,但是结果是一样的。
发布于 2016-02-29 06:02:19
Mapbox自己的地质-范围插件就能做到这一点。假设您的markers对象是有效GeoJSON,您可以简单地将它传递给geojsonExtent()函数,以获得一组边界,然后可以传递给fitBounds()。
一旦加载了geojson-extent.js文件(例如,通过在HTML代码中使用<script>标记),您应该能够这样做,以便将映射与GeoJSON markers对象的边界相匹配:
map.fitBounds(geojsonExtent(markers));更新
GeoJSONLint报告说,您的markers对象不是有效的GeoJSON,因为每个位置中的元素被解释为字符串,而不是数字。如果从lon-lat坐标中删除引号,它应该工作得很好:
var markers = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"title": "Site Gallery",
"url": "\/Freelance\/art-sheffield-2016\/programme\/site-gallery\/",
"summary": "Duis arcu tortor, suscipit eget, imperdiet nec, imperdiet iaculis, ipsum. Donec id justo. Aenean tellus metus, bibendum sed, posuere ac, mattis non, nunc. Suspendisse feugiat. Etiam rhoncus.",
"image": "\/Freelance\/art-sheffield-2016\/site\/assets\/files\/1032\/site_gallery.jpg",
"marker-symbol": "venue-map-icon-blue",
"colour": "blue"
},
"geometry": {
"type": "Point",
"coordinates": [
-1.466439,
53.376842
]
}
},
{
"type": "Feature",
"properties": {
"title": "Moore Street Substation",
"url": "\/Freelance\/art-sheffield-2016\/programme\/moore-street-substation\/",
"summary": "",
"image": null,
"marker-symbol": "venue-map-icon-green",
"colour": "green"
},
"geometry": {
"type": "Point",
"coordinates": [
-1.477881,
53.374798
]
}
},
{
"type": "Feature",
"properties": {
"title": "S1 Artspace",
"url": "\/Freelance\/art-sheffield-2016\/programme\/s1-artspace\/",
"summary": "",
"image": null,
"marker-symbol": "venue-map-icon-red",
"colour": "red"
},
"geometry": {
"type": "Point",
"coordinates": [
-1.459620,
53.380562
]
}
}
]
};
https://stackoverflow.com/questions/35586360
复制相似问题