question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to access the map.flyTo method and use it dynamically ?

See original GitHub issue

@alex3165 I’m currently using the map.flyTo inside the onStyleLoad={map => { }}

It’s working fine but, the flyTo happens at the initial render

my goal is to update the map.flyTo options dynamically on button Click?

how can I achieve that? is the map.flyTo provided as a prop?

here’s my current code:

constructor(props) {
  super(props);
    this.state = {
      flyTo: { center: [-118.4107187, 33.9415889], zoom: 11, speed: 0.4 }
    }
      ...
      ...
    
    handleClickFlyDestinationAirport = () => {
      this.setState({ flyTo: { center: [-122.404357, 37.791246], zoom: 11, speed: 0.4 } })
    }

    <Map
       style="mapbox://styles/myAccount/vfvjiej45de7k"
       center={this.state.start}
       zoom={[9.8]}
       movingMethod="flyTo"
       containerStyle={{
         height: '100vh',
         width: '100vw'
       }}
       onStyleLoad={map => {
          map.addLayer({
             id: 'routes',
             type: 'line',
             source: {
               type: 'geojson',
                  data: {
                    type: 'FeatureCollection',
                    'features': [{
                        type: 'Feature',
                        properties: {
                          'color': 'blue',
                        },
                        geometry: this.state.geoJsonData1
                    }, {
                        type: 'Feature',
                        properties: {
                          'color': '#FF793F',
                        },
                        geometry: {
                            "type": "LineString",
                            "coordinates": [
                              this.state.start2,
                              this.state.destination2
                            ]
                        }
                    }, {
                        type: 'Feature',
                        properties: {
                          'color': 'green',
                        },
                        geometry: this.state.geoJsonData2
                    }]
                  }
                },
                paint: {
                  'line-width': 3,
                  'line-color': {
                    'type': 'identity',
                    'property': 'color'
                  }
                }
              }),
              map.flyTo(this.state.flyTo);
          }}
        />

<button className={css(styles.flyToButtonMap)} onClick={this.handleClickFlyDestinationAirport}>Fly to the dest Airport</button>

screen shot 2017-12-23 at 5 43 54 pm

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:14 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
aderaaijcommented, Dec 27, 2017

@aziz-boudi4 The actual map object is not this.map, the ref you get is a ref to the React component, not to the map itself. You can find the map in this.map.state.map. Just console.log this.map and you’ll see what’s available!

3reactions
khollundcommented, Jan 4, 2018

I think the “mistake” here is the way you look at flyto. You should think of flyTo not as a method of changing the center point or other properties of the map, but an animation method to move the camera. As soon as you need to use interactions to move the map it’s important to grasp the concept of the camera being your view into the map.

So if you want to fly somewhere, in reality what you essentially want is to update position, bearing, zoom etc and move from current state to next state with a flyto animation type. And there’s many ways to do that. Fit bounds is one, depends a bit what you are trying to do. Looks like you can get away with just updating position though.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to access the map.flyTo method and use it dynamically ? -
@alex3165. I'm currently using the map.flyTo inside the onStyleLoad={map => { }}. It's working fine but, the flyTo happens at the initial render....
Read more >
How to flyTo and then getBounds around location based on ...
You need either a group of markers to get its bounds or another group of geographic entities to use effectively fitBounds around a...
Read more >
Slowly fly to a location | Mapbox GL JS
This example uses flyTo with flyOptions to slowly zoom to a location, and creates a custom atmosphere effect using setFog .
Read more >
Using a variable for map.setcenter in mapbox gl js
or use MapboxGL flyTo method map.flyTo({center:[lng, lat]}); ... You can use the split function to get an array assuming your location param ...
Read more >
react-map-gl | default (Map) - GitHub Pages
Calling the method directly may cause the the React prop to mismatch with the underlying ... By default, it loads the mapbox-gl module...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found