-
-
Notifications
You must be signed in to change notification settings - Fork 853
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implemented web MarkerView poc * feat(web): added web example for marker view --------- Co-authored-by: Volker Lieber <[email protected]>
- Loading branch information
1 parent
18bc6d8
commit 460110a
Showing
11 changed files
with
177 additions
and
15 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
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
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,8 @@ | ||
export { default as AnimatedLine } from './AnimatedLine'; | ||
export { default as AnimatedLineOffsets } from './AnimatedLineOffsets'; | ||
export { default as AnimatedPoint } from './AnimatedPoint'; | ||
export { default as DriveTheLine } from './DriveTheLine'; | ||
|
||
export const metadata = { | ||
title: 'Animations', | ||
}; |
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 React from 'react'; | ||
import { View, Text } from 'react-native'; | ||
import { MapView, Camera, MarkerView } from '@rnmapbox/maps'; | ||
|
||
function MarkerViewExample() { | ||
return ( | ||
<MapView style={{ flex: 1 }}> | ||
<Camera zoomLevel={9} centerCoordinate={[-73.970895, 40.723279]} /> | ||
<MarkerView coordinate={[-73.970895, 40.723279]}> | ||
<View style={{ backgroundColor: 'red', padding: 8 }}> | ||
<Text>Hello</Text> | ||
</View> | ||
</MarkerView> | ||
</MapView> | ||
); | ||
} | ||
|
||
export default MarkerViewExample; | ||
|
||
/* end-example-doc */ | ||
|
||
/** @type ExampleWithMetadata['metadata'] */ | ||
const metadata = { | ||
title: 'MarkerView', | ||
tags: ['MarkerView'], | ||
docs: ` | ||
Shows MarkerView | ||
`, | ||
}; | ||
MarkerViewExample.metadata = metadata; |
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,5 @@ | ||
export { default as MarkerView } from './MarkerView'; | ||
|
||
export const metadata = { | ||
title: 'Web', | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { Marker } from 'mapbox-gl'; | ||
import { | ||
forwardRef, | ||
isValidElement, | ||
memo, | ||
ReactElement, | ||
Ref, | ||
useContext, | ||
useEffect, | ||
useImperativeHandle, | ||
useMemo, | ||
} from 'react'; | ||
import { createPortal } from 'react-dom'; | ||
|
||
import MapContext from '../MapContext'; | ||
|
||
type MarkerViewProps = { | ||
coordinate: [number, number]; | ||
children?: ReactElement; | ||
}; | ||
|
||
function MarkerView(props: MarkerViewProps, ref: Ref<Marker>) { | ||
const { map } = useContext(MapContext); | ||
|
||
// Create marker instance | ||
const marker: Marker = useMemo(() => { | ||
const _marker = new Marker({ | ||
element: isValidElement(props.children) | ||
? document.createElement('div') | ||
: undefined, | ||
}); | ||
|
||
// Set marker coordinates | ||
_marker.setLngLat(props.coordinate); | ||
|
||
// Fix marker position | ||
const { style } = marker.getElement(); | ||
style.position = 'absolute'; | ||
style.top = '0'; | ||
style.left = '0'; | ||
|
||
return _marker; | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
// Add marker to map | ||
useEffect(() => { | ||
if (map === undefined) { | ||
return; | ||
} | ||
|
||
marker.addTo(map); | ||
|
||
return () => { | ||
marker.remove(); | ||
}; | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [map]); | ||
|
||
// Expose marker instance | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
useImperativeHandle(ref, () => marker, []); | ||
|
||
// Update marker coordinates | ||
const markerCoordinate = marker.getLngLat(); | ||
if ( | ||
markerCoordinate.lng !== props.coordinate[0] || | ||
markerCoordinate.lat !== props.coordinate[1] | ||
) { | ||
marker.setLngLat([props.coordinate[0], props.coordinate[1]]); | ||
} | ||
|
||
// Inject children into marker element | ||
return createPortal(props.children, marker.getElement()); | ||
} | ||
|
||
export default memo(forwardRef(MarkerView)); |
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,18 +1,20 @@ | ||
import MapboxModule from './MapboxModule'; | ||
import Camera from './components/Camera'; | ||
import MapView from './components/MapView'; | ||
import MarkerView from './components/MarkerView'; | ||
import Logger from './utils/Logger'; | ||
|
||
const ExportedComponents = { | ||
Camera, | ||
MapView, | ||
Logger, | ||
MarkerView, | ||
}; | ||
|
||
const Mapbox = { | ||
...MapboxModule, | ||
...ExportedComponents, | ||
}; | ||
|
||
export { Camera, MapView, Logger }; | ||
export { Camera, Logger, MapView, MarkerView }; | ||
export default Mapbox; |