Skip to content

Commit

Permalink
add the sites on the map
Browse files Browse the repository at this point in the history
  • Loading branch information
CloudLun committed Feb 27, 2024
1 parent 068869a commit 5cdca0e
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 41 deletions.
28 changes: 25 additions & 3 deletions components/map/Map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,28 @@ import mapboxgl, { MapMouseEvent } from 'mapbox-gl'

import { MapContext, MapContextType } from '@/contexts/MapContext'
import { StreetViewContext, StreetViewType } from '@/contexts/StreetViewContext'
import { MarkerContext, MarkerContextType } from '@/contexts/MarkerContext'

import MapLayer from './mapLayer/MapLayer'
import useCreateMarker from '@/hooks/useCreateMarker'
import { markerCreator } from '@/utils/markerCreator'


import coastalFlooding from "../../public/data/CoastalFlood.geo.json"
import justiceArea from "../../public/data/EnvironmentalJusticeArea.geo.json"
import evacuationZone from "../../public/data/HurricaneEvacuationZones.geo.json"
import neightborhood from "../../public/data/2020_nys_neigborhood.geo.json"
import sites from "../../public/data/floodgen_sites.geo.json"

import directionSVG from "../../public/icons/direction.svg"
import markerSVG from "../../public/icons/marker.svg"


const Map = () => {

const mapContainer = useRef<HTMLInputElement>(null)
const { setMap } = useContext(MapContext) as MapContextType
const { openStreetView, setOpenStreetView } = useContext(StreetViewContext) as StreetViewType
const { setDirection, setMarker, setDirectionDegree } = useContext(MarkerContext) as MarkerContextType

const [lng, setLng] = useState(-73.913);
const [lat, setLat] = useState(40.763);
Expand Down Expand Up @@ -83,6 +89,20 @@ const Map = () => {
data: sites as GeoJSON.FeatureCollection
})

let directionImg = new Image(50, 50)
let markerImg = new Image(25, 25)

directionImg.onload = () => m?.addImage('direcitonImg', directionImg, {
sdf: true
})
directionImg.src = directionSVG.src

markerImg.onload = () => m?.addImage('markerImg', markerImg, {
sdf: true
})
markerImg.src = markerSVG.src


m.addLayer({
id: 'coastal_flooding',
type: 'fill',
Expand Down Expand Up @@ -192,8 +212,10 @@ const Map = () => {
});
}, 1500)

useCreateMarker(e, m)

const { direction, marker } = markerCreator(e, m, directionImg, markerImg)
setDirectionDegree(0)
setDirection(direction),
setMarker(marker)
})


Expand Down
6 changes: 4 additions & 2 deletions components/streetView/StreetView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,22 @@ const StreetView = () => {
const degree = 45
switch (o) {
case 'previous':
marker!.setRotation(directionDegree - degree)
direction!.setRotation(directionDegree - degree)
setDirectionDegree(curr => curr - degree)
break
case 'next':
marker!.setRotation(directionDegree + degree)
direction!.setRotation(directionDegree + degree)
setDirectionDegree(curr => curr + degree)
break
}
}

const closeStreetViewClickHandler = () => {
setOpenStreetView(false)
setDirectionDegree(0)
marker?.remove()
direction?.remove()

}

return (
Expand Down
6 changes: 5 additions & 1 deletion contexts/MapContext.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { createContext, useState, Dispatch, SetStateAction, ReactNode } from "react";

import directionSVG from "../public/icons/direction.svg"
import markerSVG from "../public/icons/marker.svg"

import mapboxgl from 'mapbox-gl';


Expand All @@ -19,7 +22,8 @@ const MapProvider = ({ children }: Props) => {
const [map, setMap] = useState<mapboxgl.Map | null>(null)


return <MapContext.Provider value={{ map, setMap }} >

return <MapContext.Provider value={{ map, setMap}} >
{children}
</MapContext.Provider>
}
Expand Down
3 changes: 3 additions & 0 deletions contexts/MarkerContext.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createContext, useState, Dispatch, SetStateAction, ReactNode } from "react";


export type MarkerContextType = {
marker: mapboxgl.Marker | null,
setMarker: Dispatch<SetStateAction<mapboxgl.Marker | null>>
Expand All @@ -21,6 +22,8 @@ const MarkerProvider = ({ children }: Props) => {
const [direction, setDirection] = useState<mapboxgl.Marker | null>(null)
const [directionDegree, setDirectionDegree] = useState(0)



return <MarkerContext.Provider value={{ marker, setMarker, direction, setDirection,directionDegree, setDirectionDegree }} >
{children}
</MarkerContext.Provider>
Expand Down
35 changes: 0 additions & 35 deletions hooks/useCreateMarker.tsx

This file was deleted.

20 changes: 20 additions & 0 deletions utils/markerCreator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import mapboxgl, { MapMouseEvent } from "mapbox-gl";

import directionSVG from "../public/icons/direction.svg"
import markerSVG from "../public/icons/marker.svg"

export const markerCreator = (e: MapMouseEvent, m: mapboxgl.Map, directionImg:HTMLElement, markerImg:HTMLElement) => {

const direction = new mapboxgl.Marker(directionImg, {
offset: [-.5, -25]
}).setLngLat([e.lngLat.lng, e.lngLat.lat]).addTo(m)


const marker = new mapboxgl.Marker(markerImg, {
offset: [0, 0]
}).setLngLat([e.lngLat.lng, e.lngLat.lat]).addTo(m)

return { direction, marker }

}

0 comments on commit 5cdca0e

Please sign in to comment.