Replies: 1 comment
-
|
You can use import { Map, Source, Layer } from 'react-map-gl';
import patternSrc from './pattern.png'; // any local image
export default function Example() {
const handleMapLoad = async (event) => {
const map = event.target;
// Load the image and register it in the map style
if (!map.hasImage('my-pattern')) {
const img = await loadImage(map, patternSrc);
map.addImage('my-pattern', img);
}
// After this, any layer referencing "my-pattern" in fill-pattern will work
};
return (
<Map
mapStyle="mapbox://styles/mapbox/light-v11"
onLoad={handleMapLoad}
{...yourMapProps}
>
<Source
id="my-geojson"
type="geojson"
data={yourGeoJson}
/>
<Layer
id="pattern-fill"
type="fill"
source="my-geojson"
paint={{
'fill-pattern': 'my-pattern'
}}
/>
</Map>
);
}
// Helper: load image via mapbox-gl API
function loadImage(map, src) {
return new Promise((resolve, reject) => {
map.loadImage(src, (err, data) => {
if (err) reject(err);
else resolve(data);
});
});
}
If you try to define the |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to use
fill-patternproperty ofpaintwith some vector tile layers, and not having much success. The key difference withfill-color(which is working fine) is thatfill-patternneeds to load an image before it can be used. This seems simple enough in imperative maplibre gl codemap.loadImage()/map.addImage(), but my attempts with react have just led to race conditions on the image resource.Has anyone got
fill-patternworking with react-map-gl? Or have any ideas how to?Beta Was this translation helpful? Give feedback.
All reactions