For anyone attempting to use leaflet in an expo react native app, the marker image also won't load. A workaround is to use expo-asset, npx expo install expo-asset
, and then place an icon in the assets folder. Then you'll need to get a uri using expo-asset
and use that as the iconUrl
for the leaflet icon.
import { MapContainer, TileLayer, Marker, Popup } from "react-leaflet";
import "leaflet/dist/leaflet.css";
import L from "leaflet";
import { Asset } from "expo-asset";
import React from "react";
import { StyleSheet } from "react-native";
const WebMap = () => {
const icon = require('../../../assets/icon.png');
const iconURI = Asset.fromModule(icon).uri;
const leafletIcon = new L.Icon({
iconUrl: iconURI,
iconSize: [30, 30],
iconAnchor: [22, 94],
popupAnchor: [-3, -76],
});
return (
<MapContainer
center={[51.505, -0.09]}
zoom={13}
scrollWheelZoom={true}
style={styles.container}
>
<TileLayer
url="http://{s}.tile.osm.org/{z}/{x}/{y}.png"
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
/>
<Marker position={[51.505, -0.09]} icon={leafletIcon}>
<Popup>
A pretty CSS3 popup. <br /> Easily customizable.
</Popup>
</Marker>
</MapContainer>
);
};
const styles = StyleSheet.create({
container: {
width: "500px",
height: "500px",
},
});
export default WebMap;