Search code examples
react-nativeexporeact-native-mapsreact-native-webexpo-web

Map for React Native Web With Expo and Metro


I am building a hybrid app with React Native Expo. My struggle is that my app needs a map and needs to be functional on mobile and web. To have a map with React Native I found several libraries, but there is always a problem :

I tried all these libraries but I was not being able to achieve the rendering of my map in web.

My goal is just to use a map with React Native Expo in web using Metro. Maybe I'm missing something, but I really don't know how to display a map. Any help is welcome. Thanks in advance.


Solution

  • I found that we can use @teovilla/react-native-web-maps simply with Metro too. To do so, we can create a mymap.web.js and mymap.js component. React Native will be smart enough to pick the proper extension for the platform being built. In the first component we can import @teovilla/react-native-web-maps and in the second we can import react-native-maps.

    // /components/mymap.web.js
    import MapView from "@teovilla/react-native-web-maps";
    export default MapView;
    
    // /components/mymap.js
    import MapView from 'react-native-maps';
    export default MapView;
    

    Now in our code, we can import our component like so (path to adapt) :

    import MapView from '../components/mymap';