basic map results + improved fetching from piped
This commit is contained in:
parent
1efca320c8
commit
6c9ec56327
9 changed files with 239 additions and 20 deletions
71
map.go
Normal file
71
map.go
Normal file
|
@ -0,0 +1,71 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type NominatimResponse struct {
|
||||
Lat string `json:"lat"`
|
||||
Lon string `json:"lon"`
|
||||
}
|
||||
|
||||
func geocodeQuery(query string) (latitude, longitude string, err error) {
|
||||
// URL encode the query
|
||||
query = url.QueryEscape(query)
|
||||
|
||||
// Construct the request URL
|
||||
urlString := fmt.Sprintf("https://nominatim.openstreetmap.org/search?format=json&q=%s", query)
|
||||
|
||||
// Make the HTTP GET request
|
||||
resp, err := http.Get(urlString)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Read the response
|
||||
var result []NominatimResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
// Check if there are any results
|
||||
if len(result) > 0 {
|
||||
latitude = result[0].Lat
|
||||
longitude = result[0].Lon
|
||||
return latitude, longitude, nil
|
||||
}
|
||||
|
||||
return "", "", fmt.Errorf("no results found")
|
||||
}
|
||||
|
||||
func handleMapSearch(w http.ResponseWriter, query string, lang string) {
|
||||
// Geocode the query to get coordinates
|
||||
latitude, longitude, 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)
|
||||
return
|
||||
}
|
||||
|
||||
// Use a template to serve the map page
|
||||
data := map[string]interface{}{
|
||||
"Query": query,
|
||||
"Latitude": latitude,
|
||||
"Longitude": longitude,
|
||||
}
|
||||
|
||||
tmpl, err := template.ParseFiles("templates/map.html")
|
||||
if err != nil {
|
||||
log.Printf("Error loading map template: %v", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
tmpl.Execute(w, data)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue