map improvements + added forums search

This commit is contained in:
partisan 2024-05-21 21:22:36 +02:00
parent c848c72aea
commit 8da387f8e9
12 changed files with 424 additions and 65 deletions

13
map.go
View file

@ -14,7 +14,7 @@ type NominatimResponse struct {
Lon string `json:"lon"`
}
func geocodeQuery(query string) (latitude, longitude string, err error) {
func geocodeQuery(query string) (latitude, longitude string, found bool, err error) {
// URL encode the query
query = url.QueryEscape(query)
@ -24,29 +24,29 @@ func geocodeQuery(query string) (latitude, longitude string, err error) {
// Make the HTTP GET request
resp, err := http.Get(urlString)
if err != nil {
return "", "", err
return "", "", false, err
}
defer resp.Body.Close()
// Read the response
var result []NominatimResponse
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return "", "", err
return "", "", false, err
}
// Check if there are any results
if len(result) > 0 {
latitude = result[0].Lat
longitude = result[0].Lon
return latitude, longitude, nil
return latitude, longitude, true, nil
}
return "", "", fmt.Errorf("no results found")
return "", "", false, nil
}
func handleMapSearch(w http.ResponseWriter, query string, lang string) {
// Geocode the query to get coordinates
latitude, longitude, err := geocodeQuery(query)
latitude, longitude, found, err := geocodeQuery(query)
if err != nil {
log.Printf("Error geocoding query: %s, error: %v", query, err)
http.Error(w, "Failed to find location", http.StatusInternalServerError)
@ -58,6 +58,7 @@ func handleMapSearch(w http.ResponseWriter, query string, lang string) {
"Query": query,
"Latitude": latitude,
"Longitude": longitude,
"Found": found,
}
tmpl, err := template.ParseFiles("templates/map.html")