So I'm really new to react and leaflet but all I want to do is basically have the user enter some input, and after they press enter, trigger an event which then flies to the coordinates generated from that input. I am using geocode and the lat long coordinates are successfully generating. However I a not sure how to make the map fly to that location. Here is what I have so far:
import './App.css';
import * as React from "react";
import { ChakraProvider } from "@chakra-ui/react";
import { MapContainer, TileLayer, Marker, Popup, useMapEvents } from 'react-leaflet'
import 'leaflet/dist/leaflet.css'
import Geocode from "react-geocode";
import SearchBar from './SearchBar';
class App extends React.Component {
constructor(props){
super(props)
this.state = {
position: [43.653225, -79.383186]
}
}
mapRef = React.createRef();
changePos (pos) {
this.setState({position: pos});
this.mapRef.current.flyTo(pos);
}
render () {
return (
<ChakraProvider resetCSS = {false}>
<div className = "App">
<div id="title">
<h1>
CovidStopSpots
</h1>
<p>A responsive tracker for Covid-19.</p>
<SearchBar changePos = {this.changePos.bind(this)}></SearchBar>
</div>
<div id="map">
<MapContainer center={this.state.position} zoom={13} scrollWheelZoom={false} ref={this.mapRef}>
<TileLayer
attribution='© <a href=";>OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap/{z}/{x}/{y}.png"
/>
<Marker position={[43.653225, -79.383186]}>
<Popup>
A pretty CSS3 popup. <br /> Easily customizable.
</Popup>
</Marker>
</MapContainer>
</div>
</div>
</ChakraProvider>
)
}
}
export default App;
also the current code is generating this error:
Unhandled Rejection (TypeError): Cannot read property 'flyTo' of null
So I'm really new to react and leaflet but all I want to do is basically have the user enter some input, and after they press enter, trigger an event which then flies to the coordinates generated from that input. I am using geocode and the lat long coordinates are successfully generating. However I a not sure how to make the map fly to that location. Here is what I have so far:
import './App.css';
import * as React from "react";
import { ChakraProvider } from "@chakra-ui/react";
import { MapContainer, TileLayer, Marker, Popup, useMapEvents } from 'react-leaflet'
import 'leaflet/dist/leaflet.css'
import Geocode from "react-geocode";
import SearchBar from './SearchBar';
class App extends React.Component {
constructor(props){
super(props)
this.state = {
position: [43.653225, -79.383186]
}
}
mapRef = React.createRef();
changePos (pos) {
this.setState({position: pos});
this.mapRef.current.flyTo(pos);
}
render () {
return (
<ChakraProvider resetCSS = {false}>
<div className = "App">
<div id="title">
<h1>
CovidStopSpots
</h1>
<p>A responsive tracker for Covid-19.</p>
<SearchBar changePos = {this.changePos.bind(this)}></SearchBar>
</div>
<div id="map">
<MapContainer center={this.state.position} zoom={13} scrollWheelZoom={false} ref={this.mapRef}>
<TileLayer
attribution='© <a href="http://osm/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap/{z}/{x}/{y}.png"
/>
<Marker position={[43.653225, -79.383186]}>
<Popup>
A pretty CSS3 popup. <br /> Easily customizable.
</Popup>
</Marker>
</MapContainer>
</div>
</div>
</ChakraProvider>
)
}
}
export default App;
also the current code is generating this error:
Unhandled Rejection (TypeError): Cannot read property 'flyTo' of null
Share
Improve this question
edited Feb 19, 2021 at 20:50
kboul
14.6k5 gold badges47 silver badges58 bronze badges
asked Feb 19, 2021 at 6:10
user10231058user10231058
1211 gold badge3 silver badges10 bronze badges
1
- why don't you use only set state part to change the position? also read the position from the state in the Marker. you already hardcoded it. – Ehsan Commented Feb 19, 2021 at 7:42
1 Answer
Reset to default 8In react-leaflet version 3 you can take the map instance using whenCreated
prop and then use it to fly to another location when you do not want to use it for a ponent that is MapContainer's
child.
this.state = {
position: [43.653225, -79.383186],
map: null
}
Remove ref and use whenCreated
prop
<MapContainer center={this.state.position} zoom={13} scrollWheelZoom={false} whenCreated={map => this.setState({ map })}>
and then on your changePos event use this.state.map to fly
changePos (pos) {
this.setState({position: pos});
const {map} = this.state;
if (map) map.flyTo(pos);
}