react-native-maps: fitToElements not working
in componentDidMount i set fitToElements(true) but the map doesn’t fit all markers to screen.
Here my code:
componentDidMount(){
let _markers = [];
for (let i = 0; i < this.props.locations.length; i++) {
let _placeMarker = {
coordinate : {
latitude: this.props.locations[i].latitude,
longitude: this.props.locations[i].longitude
},
title: this.props.locations[i].name,
color: 'blue',
key: i + 1
};
_markers.push(_placeMarker);
};
this.setState({
region: {
latitude: this.props.locations[0].latitude,
longitude: this.props.locations[0].longitude,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA
},
markers: _markers
});
this.refs.map.fitToElements(true);
}
And in the render()
<View>
<MapView
ref="map"
provider={this.props.provider}
style={styles.map}
initialRegion={this.state.region}
loadingEnabled={true}
onPress={(e) => this.onMapPress(e)}
>
{this.state.markers.map(marker => (
<MapView.Marker
title={marker.title}
key={marker.key}
coordinate={marker.coordinate}
pinColor={marker.color}
/>
))}
</MapView>
</View>
But my map screen only show the initialRegion marker, i must zoom out the map to show all markers
I want to the map look like:

About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 1
- Comments: 16 (1 by maintainers)
ref works for me on both Android and iOS
try this
I have the same problem… But
fitToCoordinatesis working!closing as per https://github.com/react-native-community/react-native-maps/issues/1149#issuecomment-478473558
I am having the same issue as @alvelig ,
fitToSuppliedMarkersworks perfectly on iOS but fails on Android.Using a timeout like @alvelig works as a quick fix for the time being, does anyone know of a more robust solution?