-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented the initial version of the map needed to specify the loca…
…tion of an event
- Loading branch information
1 parent
eda5bcb
commit 8209458
Showing
6 changed files
with
147 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
export async function getLocationMatches(query){ | ||
return await fetch("https://nominatim.openstreetmap.org/search?q="+query+"&limit=5&format=json") | ||
.then(response=>response.json()) | ||
.then(r=>r.map(d=>({lat:d.lat,lng:d.lon,name:d.display_name}))) | ||
|
||
} |
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,95 @@ | ||
import 'leaflet/dist/leaflet.css'; | ||
import {MapContainer, TileLayer, Marker, Popup, useMap} from 'react-leaflet'; | ||
import * as React from "react"; | ||
import {useMemo, useRef, useState} from "react"; | ||
import "leaflet/dist/images/marker-shadow.png"; | ||
import "leaflet/dist/images/marker-icon-2x.png"; | ||
import L from 'leaflet'; | ||
import icon from 'leaflet/dist/images/marker-icon.png'; | ||
import iconShadow from 'leaflet/dist/images/marker-shadow.png'; | ||
import {Autocomplete} from "@mui/lab"; | ||
import {TextField} from "@mui/material"; | ||
import {getLocationMatches} from "../../Controllers/GeocodingController"; | ||
|
||
export default function MapWithMarker() { | ||
let DefaultIcon = L.icon({ | ||
iconUrl: icon, | ||
shadowUrl: iconShadow | ||
}); | ||
L.Marker.prototype.options.icon = DefaultIcon; | ||
const centerValue = { | ||
lat:41.0082, | ||
lng:28.9784 | ||
} | ||
const [location, setLocation] = useState(null); | ||
const [locationText, setLocationText] = useState(""); | ||
const [locations, setLocations] = React.useState([{}]); | ||
const [position, setPosition] = useState(centerValue); | ||
const markerRef = useRef(null); | ||
const eventHandlers = useMemo( | ||
() => ({ | ||
dragend() { | ||
const marker = markerRef.current | ||
if (marker != null) { | ||
setPosition(marker.getLatLng()) | ||
} | ||
}, | ||
}), | ||
[], | ||
) | ||
|
||
function SetViewOnClick({ coords }) { | ||
const map = useMap(); | ||
map.setView(coords, map.getZoom()); | ||
console.log(position) | ||
return null; | ||
} | ||
const handleLocationSearch = (event,inputQuery) => { | ||
setLocationText(inputQuery) | ||
if(inputQuery&&inputQuery.length>=3){ | ||
getLocationMatches(inputQuery).then(results=> { | ||
setLocations(results) | ||
} | ||
) | ||
} | ||
else{ | ||
setLocations([]) | ||
} | ||
} | ||
const selectLocation = (input) => { | ||
let coords = {lat:input.lat, lng:input.lng} | ||
setPosition(coords) | ||
} | ||
return ( | ||
<div> | ||
<Autocomplete | ||
value={location} | ||
inputValue={locationText} | ||
options={locations} | ||
getOptionLabel={d=>d.name ||""} | ||
filterOptions={(x)=>x} | ||
onChange={(event, value) =>value ? selectLocation(value) : selectLocation(event.target.value)} | ||
onInputChange={handleLocationSearch} | ||
renderInput={params => { | ||
return ( | ||
<TextField {...params} id="location" fullWidth label="Event Location" placeholder="Search for city, district, street..." size="small" variant="outlined" | ||
style={{marginTop: 10, marginBottom: 10}} value={locationText|""}></TextField> | ||
)}} /> | ||
<div id="map"> | ||
<MapContainer style={{width: "100%", height: "70vh"}} center={centerValue} zoom={13} | ||
scrollWheelZoom={false} attributionControl={false}> | ||
<TileLayer | ||
attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors' | ||
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" | ||
/> | ||
<Marker | ||
position={position} | ||
draggable={"true"} | ||
ref={markerRef} | ||
eventHandlers={eventHandlers}> | ||
<SetViewOnClick coords={position}/> | ||
</Marker> | ||
</MapContainer> | ||
</div> | ||
</div>); | ||
} |
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