Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Native-Maps fits Coordinate right after being loaded

The example provided in the examples of the react-native-maps repo on GitHub shows a button to execute a function to set the appropriate zoom considering to a list of markers:

  fitAllMarkers() {
    this.map.fitToCoordinates(MARKERS, {
      edgePadding: DEFAULT_PADDING,
      animated: true,
    });
}

https://github.com/airbnb/react-native-maps/blob/master/docs/mapview.md

Would it be possible to initialize the map with the appropriate fit given an array of markers already initialized ?

When trying to set the right fit on the componentDidMount, I am receiving:

Error using new LatLntBounds(LatLngBounds, int): Map size can't be 0. Most likely, layout has not yet occured for the map view.

It is definitively too early to call my fitAllMarkers() function. Is there a onLoad function that can be triggered right after the map was initialized ?

like image 796
Arkon Avatar asked Jan 31 '17 00:01

Arkon


People also ask

What is alternative of map () in react-native?

Few alternatives for google maps in react native : Mapbox - https://www.npmjs.com/package/@mapbox/react-native-mapbox-gl. Yandex Map - https://www.npmjs.com/package/react-native-yandexmapkit.

How do you use fitToSuppliedMarkers in react-native maps?

The react-native-maps has a method called fitToSuppliedMarkers for this specific reason. To implement this you need to populate the identifier props of the Marker, and then call the fitToSuppliedMarkers method of the MapView using ref, when you have received your list of coordinates.

How do I get my location in Google Maps From react-native?

Import the MapView and Marker component from react-native-maps library in App. js file. MapView: It is used to display the MapView component in the project. Marker: It is used to show the red round mark to pinpoint the exact location in Google Maps.


2 Answers

fitToCoordinates does not use markers it uses an array of latitude and longitude objects (there is a specific method for that fitToSuppliedMarkers). An important thing to make it work is to give a reference to the internal MapView, to get the reference to the map.

class Map extends Component {

    constructor() {
        super()
        this.mapRef = null;
    }

    render() {
      return    <MapView style={{flex: 1}}
                    ref={(ref) => { this.mapRef = ref }}
                    onLayout = {() => this.mapRef.fitToCoordinates(this.props.myLatLongs, { edgePadding: { top: 10, right: 10, bottom: 10, left: 10 }, animated: false })}>
                    <Polyline coordinates={this.props.myLatLongs} strokeWidth={4} strokeColor="#2962FF" /> 
                </MapView>
    }
}

I leave the code here for future people that arrive to this place. this is duplicated of my answer also in the issue of the repo https://github.com/airbnb/react-native-maps/issues/1003 .

Also anyone arriving here please see updates in documentation for mapview: https://github.com/airbnb/react-native-maps/blob/master/docs/mapview.md

like image 176
Kanekotic Avatar answered Oct 07 '22 13:10

Kanekotic


When I use the onLayout prop it takes too long on fit the map to the desired coordinates.

I would prefer the onMapReady prop which fit the map to the coordinates before loads it completely.

render() {
      return (
        <MapView style={{flex: 1}}
           ref={(ref) => { this.mapRef = ref }}
           onMapReady = {() => this.mapRef.fitToCoordinates(this.props.myLatLongs, { edgePadding: { top: 10, right: 10, bottom: 10, left: 10 }, animated: false })}>
           /** ... */
        </MapView>
    }
like image 30
JP Mazza Avatar answered Oct 07 '22 13:10

JP Mazza