Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to integrate OpenStreetMap into a react-native project?

I am trying to integrate OpenStreetMap into a React Native project. But I can't find any library or anything related to React Native in their GitHub account.

The only thing I can find relating to these to topics is the link below, in which there is no proper answer.

https://www.reddit.com/r/reactnative/comments/5fi6bg/how_to_add_openstreetmap_to_a_react_native_project/

But I heard once that Mapbox uses OpenStreetMap as their source. Mapbox suggests a good way to integrate it into a React Native project:

https://github.com/mapbox/react-native-mapbox-gl

Is there a way to integrate OpenStreetMap into a React Native project or is it the case there's not proper support for it yet.

like image 665
Amal p Avatar asked Feb 20 '18 12:02

Amal p


People also ask

How do you integrate open street maps?

Go to https://www.openstreetmap.org on your browser. Navigate on the map to the location you want to embed. On the right side, click the “Share” icon, then click “HTML”

Is Google Maps better than OpenStreetMap?

OpenStreetMap has different features like local knowledge, open data and is community-driven. Google Map has more coverage as compared to OpenStreetMap in many countries, which makes it the leading map in many countries like the United States and Japan.

What is the difference between Mapbox and OpenStreetMap?

Mapbox relies on collective mapping, with OpenStreetMap being its major source of data, and this has consequences. While the OSM project has enjoyed remarkable growth over the past ten years, its coverage in regions like India and China still needs some work.


2 Answers

Using Mapbox GL

Another option is to use Mapbox GL, which is free as long as you don't use anything in the Mapbox platform. Unlike other options, this won't require an API key ([source 1] [source 2]).

There's often confusion about Mapbox, since the platform (global styles, tilesets, datasets, Mapbox Studio, etc) is paid, but Mapbox GL is a library which you may use to display content that is either self-hosted or hosted by the Mapbox platform.

Below is an snippet of an example using Stamen Watercolor tiles (which are also OSM-based):

render() {   const rasterSourceProps = {     id: 'stamenWatercolorSource',     tileUrlTemplates: [       'https://stamen-tiles.a.ssl.fastly.net/watercolor/{z}/{x}/{y}.jpg',     ],     tileSize: 256,   };    return (     <MapboxGL.MapView style={sheet.matchParent}>       <MapboxGL.Camera         zoomLevel={16}       />        <MapboxGL.RasterSource {...rasterSourceProps}>         <MapboxGL.RasterLayer           id="stamenWatercolorLayer"           sourceID="stamenWatercolorSource"           style={{rasterOpacity: this.state.opacity}}         />       </MapboxGL.RasterSource>     </MapboxGL.MapView>   ); } 

Other options

As mentioned earlier, one other option for React Native (both Android and iOS) is react-native-maps.

It's at best unclear, though, if you must use a Google API key to use this lib if you don't display a Google map: Google says "To use the Maps SDK for Android you must have an API key", but react-native-maps appears to use a drop-in Maps SDK replacement called AirMapView, which supports other providers and claims you'd need to setup the native Maps SDK on Android only "to support native Google Maps". To be on the safe side, most people end up setting up a billing account with Google to generate an API key (even if you don't plan to ever count a single map view), something which Mapbox doesn't require.

like image 117
Rafael Almeida Avatar answered Sep 20 '22 06:09

Rafael Almeida


You can use a custom Tile Overlay (OpenStreetMap too) in react-native-maps package: https://github.com/react-native-maps/react-native-maps

(scroll to Using a custom Tile Overlay section)

You need to add api key. You don't need to use Maps API at all and the key won't be used in that case.

like image 26
Andrei Konstantinov Avatar answered Sep 22 '22 06:09

Andrei Konstantinov