Search code examples
react-nativeflexboxreact-native-mapsreact-native-componentreact-native-stylesheet

React Native Maps: How to render components below the map?


I just implemented React Native Maps in my application.

My problem is that everything that I render below the maps is rendered on top of it. Let me show you what I mean:

Searchbar and random texts get rendered above the map.

My question is how do I avoid my components being rendered on top of the map? I would like for them to be rendered below.

Here is my code:

HomeScreen.js:

render() {
    return (
      <SafeContainer>
        <KeyboardAvoidingView style={styles.container}>
          <MapsContainer />
          <SearchBar icon={require("../../assets/icons/searchingGlass.png")} />
          <Text textID={"homescreen_text"}>This is going to be the HomeScreen</Text>
          <RestaurantList />
        </KeyboardAvoidingView>
      </SafeContainer>
    );
  }

const styles = StyleSheet.create({
  container: {
    flex: 1
  }
});

MapsContainer.js:

  <View style={styles.mapContainer}>
    <MapView
      style={styles.map}
      initialRegion={{
        latitude: 58.192312,
        longitude: 7.072301,
        latitudeDelta: 0.0145,
        longitudeDelta: 0.0055
      }}
    />
  </View>

const styles = StyleSheet.create({
  mapContainer: {
    position: "absolute",
    top: 0,
    left: 0,
    right: 0,
    bottom: 0,
    height: imageWidth * (2 / 3),
    backgroundColor: colors.$primaryWhite
  },
  map: {
    position: "absolute",
    top: 0,
    left: 0,
    right: 0,
    bottom: 0
  }
});

Solution

  • I found out what I was doing wrong. The maps styles I used where completely unnecessary (got them from some github issue while implementing maps for the first time :D).

    Here are the styles that I use now:

    const imageWidth = Dimensions.get("window").width;
    const styles = StyleSheet.create({
      mapContainer: {
        height: imageWidth * (2 / 3),
        backgroundColor: colors.$primaryWhite
      },
      map: {
        height: imageWidth * (2 / 3)
      }
    });