Closed andrewshrout closed 3 years ago
HI @andrewshrout, have you tried putting the Geocoder inside the StaticMap component as a child? If that doesn't work, it would help if you could provide the simplest code possible that demonstrates your issue on Code Sandbox.
https://codesandbox.io/s/testmap-v21w4?file=/src/App.js
Geocoder inside the staticmap as a child didn't work in my nextjs project. It created the map with the geocoder, but the second it moves, it breaks again. It's also not clickable. I'm guessing something is wrong with it being beneath Deck.gl's layer, and the onViewStateChange function.
I've done my best in the link above to create a super simplified version of what I'm trying to do, but I can't recreate the issues of my project. I do know that adding the geocoder in either of them breaks.
I've been reading over every single issue on this repository with a fine toothed comb, with #44 and #56 being the most useful to me. I'm a new dev in general but I guess I'm just incredibly confused and frustrated about adding this geocoder to a class component using Deck.gl.
https://codesandbox.io/s/react-map-gl-geocoder-v2-example-forked-2zbz0?file=/src/index.js
Got a somewhat working version by forking your functional component example, passing deck.gl as a child. Now there's working deck.gl, and geocoding. However I feel like I've just sidestepped my lack of understanding for why it won't work with a static map or in a class component.
And now I have to read up on how to fetch data in a functional component because I can't get my geoservers data onto the map.
Oh I see what the problem is with your class component. It's creating a new ref on every render. The ref should only be created once. Here's the old class component example that used to be in the README.
import 'mapbox-gl/dist/mapbox-gl.css'
import 'react-map-gl-geocoder/dist/mapbox-gl-geocoder.css'
import React, { Component } from 'react'
import MapGL from 'react-map-gl'
import Geocoder from 'react-map-gl-geocoder'
function getAccessToken() {
var accessToken = null;
if (typeof window !== 'undefined' && window.location) {
var match = window.location.search.match(/access_token=([^&\/]*)/);
accessToken = match && match[1];
}
if (!accessToken && typeof process !== 'undefined') {
// Note: This depends on bundler plugins (e.g. webpack) inmporting environment correctly
accessToken = accessToken || process.env.MapboxAccessToken; // eslint-disable-line
}
return accessToken || null;
}
// Ways to set Mapbox token: https://uber.github.io/react-map-gl/#/Documentation/getting-started/about-mapbox-tokens
const MAPBOX_TOKEN = getAccessToken()
class Example extends Component {
state = {
viewport: {
width: 400,
height: 400,
latitude: 37.7577,
longitude: -122.4376,
zoom: 8
}
}
mapRef = React.createRef()
componentDidMount() {
window.addEventListener('resize', this.resize)
this.resize()
}
componentWillUnmount() {
window.removeEventListener('resize', this.resize)
}
resize = () => {
this.handleViewportChange({
width: window.innerWidth,
height: window.innerHeight
})
}
handleViewportChange = (viewport) => {
this.setState({
viewport: { ...this.state.viewport, ...viewport }
})
}
// if you are happy with Geocoder default settings, you can just use handleViewportChange directly
handleGeocoderViewportChange = (viewport) => {
const geocoderDefaultOverrides = { transitionDuration: 1000 }
return this.handleViewportChange({
...viewport,
...geocoderDefaultOverrides
})
}
render() {
return (
<MapGL
ref={this.mapRef}
{...this.state.viewport}
onViewportChange={this.handleViewportChange}
mapboxApiAccessToken={MAPBOX_TOKEN}>
<Geocoder
mapRef={this.mapRef}
onViewportChange={this.handleGeocoderViewportChange}
mapboxApiAccessToken={MAPBOX_TOKEN}
/>
</MapGL>
)
}
}
export default Example
Hey I was hoping I could get some help - I'm a new dev working on a mapping app. I tried to add the geocoder to a map I've been working on, and eventually figured out that it could work with static maps. But I have spent days and I still don't understand what I have done wrong with the mapref. Upon loading the page, my map will flicker with the datapoints on it and then the entire page goes white.
I was hoping to add a geocoder to the map so that I could eventually pass requests to my geoserver and only get points nearby, but I'm at a standstill. Below is my code, and I'd appreciate anything to help me understand better.
The map and page render fine without the geocoder component, but when it loads my console lights up with a lot of information.