gpt4 book ai didi

javascript - 使用 leaflet-react 时如何调用 fitBounds() ?

转载 作者:行者123 更新时间:2023-12-03 13:22:11 25 4
gpt4 key购买 nike

我不知道如何在 Leaflet map 上调用 fitBounds()。

如果我只是使用普通传单,这个解决方案将完美工作:Zoom to fit all markers in Mapbox or Leaflet

不幸的是,我正在使用react-leaflet。

如果我只是单独使用传单,这是解决方案。

var leafletMap = new L.featureGroup([marker1, marker2, marker3]);
map.fitBounds(leafletMap.getBounds());

我认为这段代码(我的代码)this.mapRef.current.leafletElement相当于var leafletMap = new L.featureGroup([marker1, marker2, marker3]); leafletMap.getBounds();,但是map.fitBounds();相当于react-leaflet中的什么?

基本上,我试图在 map 上显示多个标记,并相应地调整 View (放大、缩小、飞向等)。

这是我的代码。

import React, { createRef, Component } from 'react'
import { Map, TileLayer, Marker, Popup, FeatureGroup } from 'react-leaflet'

export default class MasterLeafletMap extends Component {
constructor(props) {
super(props);
this.markers = this.markers.bind(this);
this.handleClick = this.handleClick.bind(this);
this.mapRef = createRef()
}

handleClick() {
const leafletMap = this.mapRef.current.leafletElement;
this.mapRef.current.fitBounds(leafletMap.getBounds()); // Doesn't work
leafletMap.fitBounds(leafletMap.getBounds()); // Doesn't work (just trying to get the bounds of the markers that are there and adjust the view)
this.mapRef.current.leafletElement.flyToBounds(leafletMap.getBounds()); // Doesn't work
}
markers() {
if (this.props.search.items instanceof Array) {
return this.props.search.items.map(function(object, i) {
const position = [object._geoloc.lat, object._geoloc.lng];
return <Marker position={position}>
<Popup>
<span>
<h4>{object.title}</h4>
{object.address}, <br /> {object.city}, {object.state}, {object.zip} <br /> {object._geoloc.lat}, {object._geoloc.lng}
</span>
</Popup>
</Marker>
})
}

}
render() {
const hasLoaded = this.props.search.items instanceof Array;
if (!hasLoaded) {
return null;
}

const position = [this.props.search.items[0]._geoloc.lat, this.props.search.items[0]._geoloc.lng];

return (
<div className="leaflet-map-container">
<div onClick={this.handleClick}>Hello</div>
<Map center={position} zoom={13} ref={this.mapRef}>
<TileLayer
attribution="&amp;copy <a href=&quot;http://osm.org/copyright&quot;>OpenStreetMap</a> contributors"
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<FeatureGroup>
{this.markers()}
</FeatureGroup>
</Map>
</div>
)
}
}

提前致谢。

最佳答案

以下是如何通过 react-leaflet 完成此操作的示例

handleClick() {
const map = this.mapRef.current.leafletElement; //get native Map instance
const group = this.groupRef.current.leafletElement; //get native featureGroup instance
map.fitBounds(group.getBounds());
}

哪里

<div>
<button onClick={this.handleClick}>Zoom</button>
<Map
center={this.props.center}
zoom={this.props.zoom}
ref={this.mapRef}
>
<TileLayer
attribution='&amp;copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<FeatureGroup ref={this.groupRef}>
{this.props.locations.map(location => (
<Marker
key={location.name}
position={{ lat: location.lat, lng: location.lng }}
>
<Popup>
<span>
<h4>{location.name}</h4>
</span>
</Popup>
</Marker>
))}
</FeatureGroup>
</Map>
</div>

对应于

var leafletMap = new L.featureGroup([marker1, marker2, marker3]);
map.fitBounds(leafletMap.getBounds());

Here is a demo

关于javascript - 使用 leaflet-react 时如何调用 fitBounds() ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50861984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com