Skip to content
This repository has been archived by the owner on Feb 2, 2021. It is now read-only.

Commit

Permalink
fix: improve direction indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Jul 31, 2019
1 parent fa713f3 commit 1929ef5
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/Map/Map.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { createRef } from 'react'
import React, { createRef, useState } from 'react'
import {
Map as LeafletMap,
TileLayer,
Expand Down Expand Up @@ -27,6 +27,7 @@ export const Map = ({
if (userZoom) {
zoom = parseInt(userZoom, 10)
}
const [mapZoom, setMapZoom] = useState(zoom)
const mapRef = createRef<LeafletMap>()
return (
<LeafletMap
Expand All @@ -43,6 +44,7 @@ export const Map = ({
'bifravst:zoom',
`${mapRef.current.viewport.zoom}`,
)
setMapZoom(mapRef.current.viewport.zoom)
}
}}
>
Expand All @@ -55,20 +57,23 @@ export const Map = ({
</Marker>
{accuracy && <Circle center={[lat, lng]} radius={accuracy} />}
{heading && (
<LeafletConsumer>
<LeafletConsumer key={mapZoom}>
{({ map }) => {
if (map) {
const { x, y } = map.project([lat, lng], zoom)
const { x, y } = map.project([lat, lng], mapZoom)
const endpoint = map.unproject(
[
x + 30 * Math.cos((((heading - 90) % 360) * Math.PI) / 180),
y + 30 * Math.sin((((heading - 90) % 360) * Math.PI) / 180),
x + (mapZoom * 3) * Math.cos((((heading - 90) % 360) * Math.PI) / 180),
y + (mapZoom * 3) * Math.sin((((heading - 90) % 360) * Math.PI) / 180),
],
zoom,
mapZoom,
)
console.log(mapZoom)
return (
<Polyline
positions={[[lat, lng], endpoint]}
weight={mapZoom > 16 ? 1 : 2}
linecap={'round'}
color={'#000000'}
/>
)
Expand Down

0 comments on commit 1929ef5

Please sign in to comment.