-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
56 changed files
with
12,411 additions
and
15,828 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,6 @@ | ||
export default { | ||
preset: "jest-expo", | ||
setupFiles: [ | ||
"./node_modules/@maplibre/maplibre-react-native/setup-jest.js", | ||
"./setup-jest.js", | ||
], | ||
setupFiles: ["./setup-jest.js"], | ||
testMatch: ["**/__tests__/**/*.js?(x)"], | ||
transformIgnorePatterns: [], | ||
moduleNameMapper: { "@wq/material": "<rootDir>/setup-jest.js" }, | ||
}; |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import mapgl from "../index.js"; | ||
import { MapProvider } from "../index.js"; | ||
|
||
test("it loads", () => { | ||
expect(mapgl.name).toBe("map-gl"); | ||
expect(MapProvider.displayName).toBe("MapProvider:wq"); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,134 @@ | ||
import React, { useEffect, useMemo, useContext } from "react"; | ||
import PropTypes from "prop-types"; | ||
import MapLibreGL from "@maplibre/maplibre-react-native"; | ||
import { findBasemapStyle } from "../util.js"; | ||
import React, { useMemo, useContext, useRef, useCallback } from "react"; | ||
import { MapView, Camera } from "@maplibre/maplibre-react-native"; | ||
import { MapContext } from "./MapProvider.js"; | ||
import { withWQ } from "@wq/react"; | ||
import { useBasemapStyle } from "../hooks.js"; | ||
import PropTypes from "prop-types"; | ||
|
||
export default function Map({ | ||
function Map({ | ||
name, | ||
initBounds, | ||
children, | ||
mapProps, | ||
containerStyle, | ||
basemap, | ||
...mapProps | ||
}) { | ||
const { | ||
accessToken = null, | ||
dragRotate: rotateEnabled, | ||
pitchWithRotate: pitchEnabled = mapProps && mapProps.dragRotate, | ||
} = mapProps || {}; | ||
useEffect(() => { | ||
MapLibreGL.setAccessToken(accessToken); | ||
}, [accessToken]); | ||
|
||
const { setInstance } = useContext(MapContext), | ||
fitBounds = useMemo(() => { | ||
const [[xmin, ymin], [xmax, ymax]] = initBounds; | ||
return { sw: [xmin, ymin], ne: [xmax, ymax] }; | ||
}, [initBounds]), | ||
style = findBasemapStyle(children), | ||
styleURL = typeof style === "string" ? style : null, | ||
styleJSON = | ||
!style || typeof style === "string" ? null : JSON.stringify(style); | ||
const mapRef = React.useRef(), | ||
setMapRef = React.useCallback((ref) => { | ||
mapRef.current = ref; | ||
if (ref && cameraRef.current) { | ||
ref.camera = cameraRef.current; | ||
} | ||
if (ref) { | ||
setInstance(ref, name); | ||
} | ||
}, []), | ||
cameraRef = React.useRef(), | ||
setCameraRef = React.useCallback((ref) => { | ||
cameraRef.current = ref; | ||
if (ref && mapRef.current) { | ||
mapRef.current.camera = ref; | ||
} | ||
}, []); | ||
mapStyle = useBasemapStyle(basemap), | ||
style = useMemo(() => { | ||
return { | ||
flex: 1, | ||
minHeight: 200, | ||
...containerStyle, | ||
}; | ||
}, [containerStyle]); | ||
|
||
containerStyle = { | ||
flex: 1, | ||
minHeight: 200, | ||
...containerStyle, | ||
}; | ||
const mapRef = useRef(), | ||
cameraRef = useRef(), | ||
mapInstance = useMemo(() => createMapInstance(mapRef, cameraRef), []), | ||
onInit = useCallback( | ||
() => setInstance(mapInstance, name), | ||
[setInstance, mapInstance] | ||
), | ||
onPress = useCallback( | ||
(e) => { | ||
if (mapInstance.handlers.click) { | ||
mapInstance.handlers.click(e); | ||
} | ||
}, | ||
[mapInstance] | ||
), | ||
onMove = useCallback( | ||
(e) => { | ||
if (mapInstance.handlers.move) { | ||
mapInstance.handlers.move(e); | ||
} | ||
}, | ||
[mapInstance] | ||
); | ||
|
||
let rotateEnabled, pitchEnabled; | ||
if (mapProps.rotateEnabled !== undefined) { | ||
rotateEnabled = mapProps.rotateEnabled; | ||
} else if (mapProps.dragRotate !== undefined) { | ||
rotateEnabled = mapProps.dragRotate; | ||
} else { | ||
rotateEnabled = false; | ||
} | ||
if (mapProps.pitchEnabled !== undefined) { | ||
pitchEnabled = mapProps.pitchEnabled; | ||
} else if (mapProps.pitchWithRotate !== undefined) { | ||
pitchEnabled = mapProps.pitchWithRotate; | ||
} else { | ||
pitchEnabled = rotateEnabled; | ||
} | ||
|
||
return ( | ||
<MapLibreGL.MapView | ||
styleURL={styleURL} | ||
styleJSON={styleJSON} | ||
ref={setMapRef} | ||
<MapView | ||
ref={mapRef} | ||
mapStyle={mapStyle} | ||
rotateEnabled={rotateEnabled} | ||
pitchEnabled={pitchEnabled} | ||
attributionEnabled={!!accessToken} | ||
logoEnabled={!!accessToken} | ||
style={containerStyle} | ||
attributionEnabled={false} | ||
logoEnabled={false} | ||
style={style} | ||
onPress={onPress} | ||
onWillStartLoadingMap={onInit} | ||
onRegionDidChange={onMove} | ||
{...mapProps} | ||
> | ||
<MapLibreGL.Camera | ||
bounds={fitBounds} | ||
ref={setCameraRef} | ||
animationDuration={0} | ||
/> | ||
<Camera ref={cameraRef} bounds={fitBounds} animationDuration={0} /> | ||
{children} | ||
</MapLibreGL.MapView> | ||
</MapView> | ||
); | ||
} | ||
|
||
Map.propTypes = { | ||
name: PropTypes.string, | ||
initBounds: PropTypes.array, | ||
children: PropTypes.node, | ||
mapProps: PropTypes.object, | ||
containerStyle: PropTypes.object, | ||
basemap: PropTypes.object, | ||
}; | ||
|
||
export default withWQ(Map); | ||
|
||
// Mimic some functions from maplibre-gl-js & react-map-gl | ||
export function createMapInstance(mapRef, cameraRef) { | ||
return { | ||
handlers: {}, | ||
on(event, handler) { | ||
if (!["click", "move"].includes(event)) { | ||
console.warn("Unsupported event: " + event); | ||
} | ||
this.handlers[event] = handler; | ||
}, | ||
off(event) { | ||
delete this.handlers[event]; | ||
}, | ||
flyTo({ center, zoom }) { | ||
this.getCamera()?.flyTo(center, zoom); | ||
}, | ||
getBounds() { | ||
return this.getMap()?.getVisibleBounds(); | ||
}, | ||
fitBounds(bounds, { padding = 0, duration = 300 }) { | ||
this.getCamera()?.fitBounds( | ||
bounds[0], | ||
bounds[1], | ||
padding, | ||
duration | ||
); | ||
}, | ||
getMap() { | ||
return mapRef.current; | ||
}, | ||
getCamera() { | ||
return cameraRef.current; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import { Fragment } from "react"; | ||
import { withWQ } from "@wq/react"; | ||
|
||
export default Fragment; | ||
export default withWQ(Fragment, "MapLayers"); |
Oops, something went wrong.