https://github.com/pavel-corsaghin/react-native-leaflet
A LeafletView component using WebView and Leaflet map for React Native applications
https://github.com/pavel-corsaghin/react-native-leaflet
leaflet leaflet-map react-native
Last synced: 6 months ago
JSON representation
A LeafletView component using WebView and Leaflet map for React Native applications
- Host: GitHub
- URL: https://github.com/pavel-corsaghin/react-native-leaflet
- Owner: pavel-corsaghin
- License: mit
- Created: 2021-09-03T17:18:21.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2025-11-27T15:27:00.000Z (8 months ago)
- Last Synced: 2025-11-29T15:19:53.040Z (8 months ago)
- Topics: leaflet, leaflet-map, react-native
- Language: HTML
- Homepage:
- Size: 4.63 MB
- Stars: 154
- Watchers: 3
- Forks: 64
- Open Issues: 22
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# react-native-leaflet
A LeafletView component using WebView and Leaflet map for React Native applications
Notes: This project is replacement for https://github.com/reggie3/react-native-webview-leaflet, which no longer maintain by author and work only with expo.
[](https://www.npmjs.com/package/react-native-leaflet-view)
[](https://www.npmjs.com/package/react-native-leaflet-view)
[](https://www.npmjs.com/package/react-native-leaflet-view)

## Installation
Install using npm or yarn like this:
```sh
npm install --save react-native-leaflet-view
```
or
```sh
yarn add react-native-leaflet-view
```
## react-native configuration
This package is required for proper file handling and WebView functionality in React Native:
- [react-native-webview](https://github.com/react-native-webview/react-native-webview). You can install it using yarn or npm:
```sh
npm install --save react-native-webview
```
or
```sh
yarn add react-native-webview
```
## Expo configuration
For Expo projects, you'll need to add additional dependencies:
```sh
npx expo install react-native-webview expo-asset expo-file-system
```
These packages are required for proper file handling and WebView functionality in Expo:
- [react-native-webview docs](https://docs.expo.dev/versions/latest/sdk/webview/)
- [expo-asset docs](https://docs.expo.dev/versions/latest/sdk/asset/)
- [expo-file-system docs](https://docs.expo.dev/versions/latest/sdk/filesystem/)
You need to do this in the root of your project: Copy the required HTML file from the package with this one-liner
```sh
cp node_modules/react-native-leaflet-view/android/src/main/assets/leaflet.html assets
```
## Usage with react-native-cli
```js
import React from 'react';
import { LeafletView } from 'react-native-leaflet-view';
const DEFAULT_LOCATION = {
latitude: -23.5489,
longitude: -46.6388
}
const App: React.FC = () => {
return (
);
}
export default App;
```
## Usage with Expo
```js
import React, { useEffect, useState } from 'react';
import { ActivityIndicator, Alert } from 'react-native';
import { Asset } from "expo-asset";
import { File } from 'expo-file-system';
import { LatLng, LeafletView } from 'react-native-leaflet-view';
const DEFAULT_LOCATION = {
latitude: -23.5489,
longitude: -46.6388
}
const App: React.FC = () => {
const [webViewContent, setWebViewContent] = useState(null);
useEffect(() => {
let isMounted = true;
const loadHtml = async () => {
try {
const path = require("./assets/leaflet.html");
const asset = Asset.fromModule(path);
await asset.downloadAsync();
const htmlContent = await new File(asset.localUri!).text();
if (isMounted) {
setWebViewContent(htmlContent);
}
} catch (error) {
Alert.alert('Error loading HTML', JSON.stringify(error));
console.error('Error loading HTML:', error);
}
};
loadHtml();
return () => {
isMounted = false;
};
}, []);
if (!webViewContent) {
return
}
return (
);
}
export default App;
```
## Props
| property | required | type | purpose |
| ------------------- | -------- | ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
| loadingIndicator | optional | React.ReactElement | custom component displayed while the map is loading |
| onError | optional | function | Will receive an error event |
| onLoadEnd | optional | function | Called when map stops loading |
| onLoadStart | optional | function | Called when the map starts to load |
| onMessageReceived | required | function | This function receives messages in the form of a WebviewLeafletMessage object from the map |
| mapLayers | optional | MapLayer array | An array of map layers |
| mapMarkers | optional | MapMarker array | An array of map markers |
| mapShapes | optional | MapShape[] | An array of map shapes |
| mapCenterPosition | optional | {lat: [Lat], lng: [Lng]} object | The center position of the map. This coordinate will not be accurate if the map has been moved manually. However, calling the map's setMapCenterPosition function will cause the map to revert to this location |
| ownPositionMarker | optional | Marker | A special marker that has an ID of OWN_POSTION_MARKER_ID |
| zoom | optional | number | Desired zoom value of the map |
| doDebug | optional | boolean | A flag for debug message logging |
| source | optional | WebView["source"] | Loads static html or a uri (with optional headers) in the WebView. |
| zoomControl | optional | boolean | Controls the visibility of the zoom controls on the map. |
| attributionControl | optional | boolean | Controls the visibility of the attribution control on the map. |
## Contributing
See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
## License
MIT