@@ -22,29 +22,26 @@ export function MapView() {
2222 const mapRef = useRef < mapboxgl . Map | null > ( null ) ;
2323 const mapContainerRef = useRef < HTMLDivElement | null > ( null ) ;
2424
25+ const [ userLocation , setUserLocation ] = useState < {
26+ lng : number ;
27+ lat : number ;
28+ } | null > ( null ) ;
2529 const [ center , setCenter ] = useState < { lng : number ; lat : number } > (
2630 INITIAL_CENTER
2731 ) ;
2832 const [ boundingBox , setBoundingBox ] = useState < LngLatLike [ ] > ( [ ] ) ;
2933 const [ zoom , setZoom ] = useState ( INITIAL_ZOOM ) ;
3034 const [ pitch , _ ] = useState ( INITIAL_PITCH ) ;
3135
32- const [ data , setData ] = useState < GeoJSON . FeatureCollection | undefined > (
33- undefined
34- ) ;
3536 const [ isSummoning , setIsSummoning ] = useState ( false ) ;
36- const [ userLocation , setUserLocation ] = useState < {
37- lng : number ;
38- lat : number ;
39- } | null > ( null ) ;
4037
4138 const mapboxToken = process . env . NEXT_PUBLIC_MAPBOX_ACCESS_TOKEN ;
4239
4340 const handleSummon = async ( ) => {
41+ toast . info ( `Summoning to ${ userLocation ?. lat } ${ userLocation ?. lng } ` )
4442 setIsSummoning ( true ) ;
4543 const coords = userLocation ? userLocation : center ;
4644
47- try {
4845 const summonReq = await summon ( coords . lng , coords . lat ) ;
4946
5047 if ( ! summonReq . ok ) {
@@ -53,45 +50,6 @@ export function MapView() {
5350 setIsSummoning ( false ) ;
5451 return ;
5552 }
56-
57- const eventSource = new EventSource ( "http://localhost:8000/api/summon" ) ;
58-
59- eventSource . onmessage = ( e ) => {
60- const parsedData = JSON . parse ( e . data ) ;
61- const { lat, lon } = parsedData . current_position ;
62-
63- setData ( {
64- type : "FeatureCollection" ,
65- features : [
66- {
67- type : "Feature" ,
68- geometry : {
69- type : "Point" ,
70- coordinates : [ lon , lat ] ,
71- } ,
72- properties : { } ,
73- } ,
74- ] ,
75- } ) ;
76- } ;
77-
78- eventSource . onerror = ( err ) => {
79- // If the EventSource is closed (readyState === 2) it is expected.
80- if ( err . eventPhase === EventSource . CLOSED ) {
81- console . log ( "Summon stream closed." ) ;
82- } else {
83- console . error ( "EventSource error:" , err ) ;
84- }
85- eventSource . close ( ) ;
86- setData ( undefined ) ;
87- setIsSummoning ( false ) ;
88- } ;
89- } catch ( error : any ) {
90- toast . error ( "Error triggering summon:" , {
91- description : error . message ,
92- } ) ;
93- setIsSummoning ( false ) ;
94- }
9553 } ;
9654
9755 const handleZoomIn = ( ) => {
@@ -112,7 +70,11 @@ export function MapView() {
11270 center : center ,
11371 zoom : zoom ,
11472 pitch : pitch ,
115- style : "mapbox://styles/mapbox/streets-v12" ,
73+ // style: "mapbox://styles/mapbox/satellite-v9",
74+ maxBounds : [
75+ [ - 88.2368 , 40.0925 ] , // Southwest coordinates
76+ [ - 88.2346 , 40.0935 ] // Northeast coordinates
77+ ]
11678 } ) ;
11779
11880 const draw = new MapboxDraw ( {
@@ -122,7 +84,7 @@ export function MapView() {
12284 polygon : true ,
12385 trash : true ,
12486 } ,
125- defaultMode : "draw_polygon " ,
87+ defaultMode : "simple_select " ,
12688 } ) ;
12789
12890 mapRef . current . on ( "load" , async ( ) => {
@@ -168,21 +130,18 @@ export function MapView() {
168130 }
169131 }
170132
171- const geolocateControl = new mapboxgl . GeolocateControl ( {
172- positionOptions : {
173- enableHighAccuracy : true ,
174- } ,
175- trackUserLocation : true ,
176- showUserHeading : true ,
177- } ) ;
178- mapRef . current . addControl ( geolocateControl ) ;
133+ const marker = new mapboxgl . Marker ( {
134+ draggable : true
135+ } )
136+ . setLngLat ( INITIAL_CENTER )
137+ . addTo ( mapRef . current ) ;
179138
180- geolocateControl . on ( "geolocate" , ( e ) => {
181- setUserLocation ( {
182- lng : e . coords . longitude ,
183- lat : e . coords . latitude ,
184- } ) ;
185- } ) ;
139+ function onDragEnd ( ) {
140+ const lngLat = marker . getLngLat ( ) ;
141+ setUserLocation ( { lat : lngLat . lat , lng : lngLat . lng } )
142+ }
143+
144+ marker . on ( 'dragend' , onDragEnd ) ;
186145
187146 mapRef . current . on ( "move" , ( ) => {
188147 // get the current center coordinates and zoom level from the map
@@ -205,27 +164,6 @@ export function MapView() {
205164 } ;
206165 } , [ ] ) ;
207166
208- useEffect ( ( ) => {
209- if ( ! data ) return ;
210-
211- const source = mapRef . current ?. getSource ( "iss" ) as mapboxgl . GeoJSONSource ;
212- if ( source ) {
213- source . setData ( data ) ;
214- }
215-
216- if (
217- data . features . length > 0 &&
218- data . features [ 0 ] . geometry . type === "Point" &&
219- Array . isArray ( data . features [ 0 ] . geometry . coordinates )
220- ) {
221- const coords = data . features [ 0 ] . geometry . coordinates as [ number , number ] ;
222- mapRef . current ?. flyTo ( {
223- center : coords ,
224- speed : 0.5 ,
225- } ) ;
226- }
227- } , [ data ] ) ;
228-
229167 return (
230168 < div className = "relative w-full h-full bg-neutral-800 flex items-center justify-center" >
231169 < div id = "map-container" ref = { mapContainerRef } />
@@ -266,7 +204,7 @@ export function MapView() {
266204 className = "rounded-
267205 full bg-neutral-900 hover:bg-neutral-800"
268206 onClick = { handleSummon }
269- disabled = { isSummoning }
207+ disabled = { isSummoning || ! userLocation }
270208 >
271209 < Locate className = "h-5 w-5 mr-2" />
272210 { isSummoning ? "Summoning..." : "Summon My Car" }
0 commit comments