Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/Annotation.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React from 'react'

import { MapContext } from './Map'

import {
createCoordinate,
propsToMarkerConstructionOptions,
AnnotationOptions,
} from './utils'

type AnnotationProps = {
latitude: number
longitude: number
factory: (
coordinate: mapkit.Coordinate,
options?: mapkit.AnnotationConstructorOptions,
) => Element
} & AnnotationOptions

export const Annotation: React.FC<AnnotationProps> = ({
latitude,
longitude,
factory,
...options
}) => {
const { mapkit, map } = React.useContext(MapContext)
const annotation = React.useRef<mapkit.Annotation>()

React.useEffect(() => {
if (mapkit && map) {
annotation.current = new mapkit.Annotation(
createCoordinate(latitude, longitude),
factory,
propsToMarkerConstructionOptions(options),
)

map.addAnnotation(annotation.current)
}
return () => { annotation.current && map && map.removeAnnotation(annotation.current) }
}, [mapkit, map])

return null
}
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './Annotation'
export * from './MapkitProvider'
export * from './Map'
export * from './Marker'
Expand Down
11 changes: 11 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ type MapConstructionOptions = NonNullable<
ConstructorParameters<typeof mapkit.Map>[1]
>

type AnnotationConstructionOptions = NonNullable<
ConstructorParameters<typeof mapkit.Annotation>[1]
>

type MarkerConstructionOptions = NonNullable<
ConstructorParameters<typeof mapkit.MarkerAnnotation>[1]
>
Expand Down Expand Up @@ -117,6 +121,13 @@ export const propsToMapConstructionOptions = ({
// 📌 Marker Options

// these are the props we expose to users.
export type AnnotationOptions = Merge<
AnnotationConstructionOptions,
{
padding?: PaddingType
}
>

export type MarkerOptions = Merge<
MarkerConstructionOptions,
{
Expand Down