我無法弄清楚如何在 Leaflet 地圖上調用 fitBounds()?;旧?,我試圖在地圖上顯示多個標記并相應地調整視圖(放大、縮小、飛到等)。我還看到了一些例子,當使用 Leaflet-react 時如何調用 fitBounds()?我試圖實施,但沒有奏效。這是我嘗試過的代碼。import React, { createRef, Component } from "react";import { Map, TileLayer, Marker, Popup} from "react-leaflet";import L from "leaflet";import Header from "../Layout/Header";import Card from "@material-ui/core/Card";import CardContent from "@material-ui/core/CardContent";import Typography from "@material-ui/core/Typography";import "leaflet/dist/leaflet.css";export class Mapp extends Component { constructor(props) { super(props); this.state = { map: [], open: false, bounds: null, center: [35.000074, 104.999927] }; this.mapRef = createRef(); this.groupRef = createRef(); } toggleHiddden1() { this.setState({ open: false }); async componentDidMount() { try { await fetch(`https://coronaviva.herokuapp.com/api/1/infected/data/`, { method: "GET", headers: { Accept: "application/json", "Content-Type": "application/json", Authorization: "Bearer F9bQK456iUpJVZJLTZsMEKhhENqnGJ" } }) .then(map => map.json()) .then(map => { this.setState({ map }); }); } catch (err) { console.log(err); } let mapInst = this.refs.map.leafletElement.fitBounds; console.log(mapInst); // i tried this but this not working. } // centerUpdated(center) { // this.center = center; // } // boundsUpdated(bounds) { // this.bounds = bounds; // } render() { const { map } = this.state; const pointerIcon = new L.Icon({ iconUrl: "https://icons.iconarchive.com/icons/paomedia/small-n-flat/512/map-marker-icon.png", iconAnchor: [25, 40], iconSize: [50, 50] }); return ( <div> <Header state={this.state} load={this.onChange} submit={this.handleSubmit} />
如何在 react-leaflet 中調用 fitBounds 和 getBounds?
慕絲7291255
2022-05-22 15:59:27