Answers:
使用@Farhat的答案,我发现我所需要的只是传递一个数组数组:
map.fitBounds([
[-4.8587000, 39.8772333],
[-6.4917667, 39.0945000]
])
map.fitBounds()
如果您正在使用Google Map Polylines,它也可以很好地工作:
<!-- required for Leaflet Polyline support -->
<script type="text/javascript" src="https://cdn.rawgit.com/jieter/Leaflet.encoded/68e78190/Polyline.encoded.js"
crossorigin=""></script>
// polylines require backslashes to be escaped (Jinja example)
let encodedRoute = '{{ activity.strava_data['map']['polyline']|replace("\\", "\\\\")|safe }}';
let coordinates = L.Polyline.fromEncoded(encodedRoute).getLatLngs();
let map = L.map('map').fitBounds(coordinates);
感谢@Mike McKay,这就是我做到的方式!;)
请注意,我在坐标中添加了一些额外内容作为内部填充,因此标记不在地图的侧面。这种方式看起来。
const [ ...coordinates ] = locations.map(marker => [ marker.coordinates.latitude +
0.055, marker.coordinates.longitude + 0.055 ])
map.fitBounds([ ...coordinates ])
VUE.JS方式:
mounted () {
this.$nextTick(() => {
this.initialZoom()
})
}
methods: {
initialZoom () {
const map = this.$refs.myMap.mapObject
const [ ...coordinates ] = this.locations.map(marker => [
marker.coordinates.latitude + 0.055,
marker.coordinates.longitude + 0.055
])
map.fitBounds([ ...coordinates ])