-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement basic map view #16
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
c09e9a9
chore(lint): adjust lint rules
33abac2
chore(deps): add peer dependencies
ba70dfc
feat: add example component
02a10d7
fix: remove old example component
b10ebf1
feat: implement basic map view
1cdb526
fix: get rid of the initial implementation
8cca324
feat: add a new util dep to get map bounds
c04374b
style(prettier): adjust prettier config
db2c1a3
feat: implement basic MapView
9dbef76
chore: remove example component
6fcd95c
style(eslint): update eslint rules
91a8665
feat: change MapView API
c76a83f
refactor: change directory structure
af149f5
Revert "feat: change MapView API"
f5a62fb
Revert "refactor: change directory structure"
313c2c0
fix: undo directory structure change
d82b4da
fix: add missing index file
6858c21
style: use default export
deec0dc
fix: update imports
fbaa463
style: change file name
352805a
style(eslint): turn an ESLint rule back on
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import {MapViewProps} from './MapViewTypes'; | ||
|
||
const DEFAULT_ZOOM = 10; | ||
const DEFAULT_COORDINATE: [number, number] = [-122.4021, 37.7911]; | ||
|
||
export const PADDING = 50; | ||
export const DEFAULT_INITIAL_STATE: MapViewProps['initialState'] = { | ||
location: DEFAULT_COORDINATE, | ||
zoom: DEFAULT_ZOOM, | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import Mapbox from '@rnmapbox/maps'; | ||
|
||
function Direction({coordinates}: {coordinates: Array<[number, number]>}) { | ||
if (coordinates.length < 1) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Mapbox.ShapeSource | ||
id="routeSource" | ||
shape={{ | ||
type: 'Feature', | ||
properties: {}, | ||
geometry: { | ||
type: 'LineString', | ||
coordinates, | ||
}, | ||
}} | ||
> | ||
<Mapbox.LineLayer | ||
id="routeFill" | ||
style={{ | ||
lineColor: 'blue', | ||
lineWidth: 3, | ||
}} | ||
/> | ||
</Mapbox.ShapeSource> | ||
); | ||
} | ||
|
||
export default Direction; |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import {Layer, Source} from 'react-map-gl'; | ||
import {View} from 'react-native'; | ||
|
||
function Direction({coordinates}: {coordinates: Array<[number, number]>}) { | ||
if (coordinates.length < 1) { | ||
return null; | ||
} | ||
return ( | ||
<View> | ||
{coordinates && ( | ||
<Source | ||
id="route" | ||
type="geojson" | ||
data={{ | ||
type: 'Feature', | ||
properties: {}, | ||
geometry: { | ||
type: 'LineString', | ||
coordinates, | ||
}, | ||
}} | ||
> | ||
<Layer | ||
id="route" | ||
type="line" | ||
source="route" | ||
layout={{'line-join': 'round', 'line-cap': 'round'}} | ||
paint={{'line-color': '#888', 'line-width': 4}} | ||
/> | ||
</Source> | ||
)} | ||
</View> | ||
); | ||
} | ||
|
||
export default Direction; |
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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import {forwardRef, useEffect, useImperativeHandle, useMemo, useRef} from 'react'; | ||
import Mapbox, {MarkerView} from '@rnmapbox/maps'; | ||
import {View} from 'react-native'; | ||
import {MapViewProps, MapViewHandle} from './MapViewTypes'; | ||
import Direction from './Direction'; | ||
import Utils from './utils'; | ||
|
||
const MapView = forwardRef<MapViewHandle, MapViewProps>(function MapView( | ||
{accessToken, style, styleURL, pitchEnabled, mapPadding, initialState, waypoints, markerComponent: MarkerComponent, directionCoordinates}, | ||
ref, | ||
) { | ||
const cameraRef = useRef<Mapbox.Camera>(null); | ||
|
||
const bounds = useMemo(() => { | ||
if (!waypoints || waypoints.length === 0) { | ||
return undefined; | ||
} | ||
|
||
if (waypoints.length === 1) { | ||
cameraRef.current?.flyTo(waypoints[0]); | ||
cameraRef.current?.zoomTo(15); | ||
return undefined; | ||
} | ||
|
||
const {southWest, northEast} = Utils.getBounds(waypoints); | ||
return { | ||
ne: northEast, | ||
sw: southWest, | ||
paddingTop: mapPadding, | ||
paddingRight: mapPadding, | ||
paddingBottom: mapPadding, | ||
paddingLeft: mapPadding, | ||
}; | ||
}, [waypoints]); | ||
|
||
useImperativeHandle( | ||
ref, | ||
() => ({ | ||
flyTo: (location: [number, number], animationDuration?: number) => cameraRef.current?.flyTo(location, animationDuration), | ||
}), | ||
[], | ||
); | ||
|
||
// Initialize Mapbox on first mount | ||
useEffect(() => { | ||
Mapbox.setAccessToken(accessToken); | ||
}, []); | ||
|
||
return ( | ||
<View style={style}> | ||
<Mapbox.MapView | ||
styleURL={styleURL} | ||
pitchEnabled={pitchEnabled} | ||
style={{flex: 1}} | ||
> | ||
<Mapbox.Camera | ||
ref={cameraRef} | ||
defaultSettings={{ | ||
centerCoordinate: initialState?.location, | ||
zoomLevel: initialState?.zoom, | ||
}} | ||
bounds={bounds} | ||
/> | ||
{MarkerComponent && | ||
waypoints && | ||
waypoints.map((waypoint) => ( | ||
<MarkerView | ||
id={`${waypoint[0]},${waypoint[1]}`} | ||
key={`${waypoint[0]},${waypoint[1]}`} | ||
coordinate={waypoint} | ||
> | ||
<MarkerComponent /> | ||
</MarkerView> | ||
))} | ||
{directionCoordinates && <Direction coordinates={directionCoordinates} />} | ||
</Mapbox.MapView> | ||
</View> | ||
); | ||
}); | ||
|
||
export default MapView; |
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 |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import Map, {MapRef, Marker} from 'react-map-gl'; | ||
import {RefObject, forwardRef, useEffect, useImperativeHandle, useRef, useState} from 'react'; | ||
import WebMercatorViewport from '@math.gl/web-mercator'; | ||
import {View} from 'react-native'; | ||
import {MapViewHandle, MapViewProps} from './MapViewTypes'; | ||
import Utils from './utils'; | ||
import 'mapbox-gl/dist/mapbox-gl.css'; | ||
import Direction from './Direction'; | ||
import {DEFAULT_INITIAL_STATE} from './CONST'; | ||
|
||
const getMapDimension = (mapRef: RefObject<MapRef>): {width: number; height: number} | undefined => { | ||
if (!mapRef.current?.getMap()) { | ||
return undefined; | ||
} | ||
|
||
const {clientWidth, clientHeight} = mapRef.current.getCanvas(); | ||
return {width: clientWidth, height: clientHeight}; | ||
}; | ||
|
||
const MapView = forwardRef<MapViewHandle, MapViewProps>(function MapView( | ||
{accessToken, waypoints, style, mapPadding, markerComponent: MarkerComponent, directionCoordinates, initialState = DEFAULT_INITIAL_STATE}, | ||
ref, | ||
) { | ||
const mapRef = useRef<MapRef>(null); | ||
const [bounds, setBounds] = useState<{ | ||
longitude: number; | ||
latitude: number; | ||
zoom: number; | ||
}>(); | ||
|
||
useEffect(() => { | ||
if (!waypoints || waypoints.length === 0) { | ||
return; | ||
} | ||
|
||
if (waypoints.length === 1) { | ||
mapRef.current?.flyTo({ | ||
center: waypoints[0], | ||
zoom: 15, | ||
}); | ||
return; | ||
} | ||
|
||
const {northEast, southWest} = Utils.getBounds(waypoints); | ||
const {width, height} = getMapDimension(mapRef) || { | ||
width: 0, | ||
height: 0, | ||
}; | ||
const viewport = new WebMercatorViewport({height, width}); | ||
|
||
const {latitude, longitude, zoom} = viewport.fitBounds([southWest, northEast], { | ||
padding: mapPadding, | ||
}); | ||
|
||
setBounds({latitude, longitude, zoom}); | ||
}, [waypoints]); | ||
|
||
useImperativeHandle( | ||
ref, | ||
() => ({ | ||
flyTo: (location: [number, number], animationDuration?: number) => | ||
mapRef.current?.flyTo({ | ||
center: location, | ||
duration: animationDuration, | ||
}), | ||
}), | ||
[], | ||
); | ||
|
||
return ( | ||
<View style={style}> | ||
<Map | ||
ref={mapRef} | ||
mapboxAccessToken={accessToken} | ||
initialViewState={{ | ||
longitude: initialState?.location[0], | ||
latitude: initialState?.location[1], | ||
zoom: initialState?.zoom, | ||
}} | ||
mapStyle="mapbox://styles/mapbox/streets-v9" | ||
{...bounds} | ||
> | ||
{MarkerComponent && | ||
waypoints && | ||
waypoints.map((waypoint) => ( | ||
<Marker | ||
key={`${waypoint[0]},${waypoint[1]}`} | ||
longitude={waypoint[0]} | ||
latitude={waypoint[1]} | ||
> | ||
<MarkerComponent /> | ||
</Marker> | ||
))} | ||
{directionCoordinates && <Direction coordinates={directionCoordinates} />} | ||
</Map> | ||
</View> | ||
); | ||
}); | ||
|
||
export default MapView; |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import {ComponentType} from 'react'; | ||
import type {StyleProp, ViewStyle} from 'react-native'; | ||
|
||
export type MapViewProps = { | ||
// Public access token to be used to fetch map data from Mapbox. | ||
accessToken: string; | ||
// Style applied to MapView component. Note some of the View Style props are not available on ViewMap | ||
style: StyleProp<ViewStyle>; | ||
// Link to the style JSON document. | ||
styleURL?: string; | ||
// Whether map can tilt in the vertical direction. | ||
pitchEnabled?: boolean; | ||
// Padding to apply when the map is adjusted to fit waypoints and directions | ||
mapPadding?: number; | ||
// Initial coordinate and zoom level | ||
initialState?: { | ||
location: [number, number]; | ||
zoom: number; | ||
}; | ||
// Locations on which to put markers | ||
waypoints?: Array<[number, number]>; | ||
// React component to use for the marker. If not provided, markers are not displayed for waypoints. | ||
markerComponent?: ComponentType; | ||
// List of coordinates which together forms a direction. | ||
directionCoordinates?: Array<[number, number]>; | ||
}; | ||
|
||
export type MapViewHandle = { | ||
flyTo: (location: [number, number], animationDuration?: number) => void; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import MapView from './MapView'; | ||
|
||
export type {MapViewProps, MapViewHandle} from './MapViewTypes'; | ||
export default MapView; |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
function getBounds(waypoints: Array<[number, number]>): {southWest: [number, number]; northEast: [number, number]} { | ||
const lngs = waypoints.map((waypoint) => waypoint[0]); | ||
const lats = waypoints.map((waypoint) => waypoint[1]); | ||
|
||
return { | ||
southWest: [Math.min(...lngs), Math.min(...lats)], | ||
northEast: [Math.max(...lngs), Math.max(...lats)], | ||
}; | ||
} | ||
|
||
export default { | ||
getBounds, | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import MapView from './MapView'; | ||
|
||
export * from './MapView'; | ||
export default MapView; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code caused Expensify/App#25732. Map box API doesn't seem to support
flyTo
well enough on native platforms.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's rather the issue with the libraries themself than a regression but yea we should have tested this thoroughly