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.

Start at the user current position

See original GitHub issue

It’s not clear from the docs how to set the initial coordinate to be the current location of the user.

The only way I could was by doing something like follows, but it looks too complicated for something that could/should be just a flag such as defaultCenterUserLocation={true}.

// Looks like centerCoordinate has to be undefined if not set, null won't work
this.state = { initialCoords: undefined }

<MapboxGL.UserLocation
  onUpdate={location => {
    const currentCoords = [location.coords.longitude, location.coords.latitude]
    const initialCoords = this.state.initialCoords

    // The idea here is initialCoords should be set only once, otherwise we'll be updating
    // the camera's centerCoordinate over and over on every onUpdate(), which causes
    // issues when doing camera.flyTo(somewhere_else) or when dragging the map manually
    // (the camera will tend to reset its center position to the current position)
    this.setState({ initialCoords: initialCoords ? initialCoords : currentCoords })
  }}
/>

<MapboxGL.Camera
  defaultSettings={{
    centerCoordinate: [0, 0],
    zoomLevel: 14
  }}
  centerCoordinate={this.state.initialCoords}
/>

I’m pretty sure it’s not the way to go so please let me know how to do this.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
marcmoocommented, Jun 8, 2020

<MapboxGL.UserLocation ref={(location) => {console.log({location})}} /> then you can get corrent location and use the ref of camera method this.camera.moveTo([lng, lat])

4reactions
briannezhadcommented, Feb 27, 2021

You guys can just use <MapboxGL.Camera followUserLocation={true} />

Read more comments on GitHub >

github_iconTop Results From Across the Web

Start at the user current position · Issue #560 · rnmapbox/maps
Ie, the center of the map will follow the user position. If you don't want that only set this.setState({ initialCoords: currentCoords }) once....
Read more >
how to get user current position as initial location in flutter ...
Give _initialPosition a starting value and then update GoogleMap when _initialPosition has been updated. Share.
Read more >
Geolocation: Displaying User or Device Position on Maps
This tutorial shows you how to display the geographic location of a user or device on a Google ... Below is a map...
Read more >
Get user's current location in android || Android studio tutorial
In This Video you will learn how to get user's current location with the help of gps in android.We will not use fusedlocationproviderclient ......
Read more >
Geolocation.getCurrentPosition() - Web APIs | MDN
The Geolocation.getCurrentPosition() method is used to get the current position of the device.
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 Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found