GoBlog/geoMap.go

99 lines
2.2 KiB
Go
Raw Normal View History

2021-07-06 19:06:39 +00:00
package main
import (
"encoding/json"
"net/http"
)
const defaultGeoMapPath = "/map"
2021-07-06 19:06:39 +00:00
func (a *goBlog) serveGeoMap(w http.ResponseWriter, r *http.Request) {
blog, bc := a.getBlog(r)
2021-07-06 19:06:39 +00:00
2021-11-22 16:19:07 +00:00
mapPath := bc.getRelativePath(defaultIfEmpty(bc.Map.Path, defaultGeoMapPath))
canonical := a.getFullAddress(mapPath)
2021-08-05 06:09:34 +00:00
allPostsWithLocation, err := a.getPosts(&postsRequestConfig{
2021-07-06 19:06:39 +00:00
blog: blog,
status: statusPublished,
2021-11-13 19:19:46 +00:00
parameters: []string{a.cfg.Micropub.LocationParam, gpxParameter},
withOnlyParameters: []string{a.cfg.Micropub.LocationParam, gpxParameter},
2021-07-06 19:06:39 +00:00
})
if err != nil {
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
return
}
if len(allPostsWithLocation) == 0 {
a.render(w, r, templateGeoMap, &renderData{
Canonical: canonical,
2021-07-06 19:06:39 +00:00
Data: map[string]interface{}{
"nolocations": true,
},
})
return
}
type templateLocation struct {
Lat float64
Lon float64
Post string
}
2021-11-13 19:19:46 +00:00
type templateTrack struct {
Paths [][]*trackPoint
Points []*trackPoint
Post string
2021-11-13 19:19:46 +00:00
}
2021-07-06 19:06:39 +00:00
var locations []*templateLocation
2021-11-13 19:19:46 +00:00
var tracks []*templateTrack
2021-07-06 19:06:39 +00:00
for _, p := range allPostsWithLocation {
if g := a.geoURI(p); g != nil {
2021-07-06 19:06:39 +00:00
locations = append(locations, &templateLocation{
Lat: g.Latitude,
Lon: g.Longitude,
Post: p.Path,
})
}
2021-11-13 19:19:46 +00:00
if t, err := a.getTrack(p); err == nil && t != nil {
tracks = append(tracks, &templateTrack{
Paths: t.Paths,
Points: t.Points,
Post: p.Path,
2021-11-13 19:19:46 +00:00
})
}
}
2021-11-13 19:40:25 +00:00
locationsJson := ""
if len(locations) > 0 {
locationsJsonBytes, err := json.Marshal(locations)
if err != nil {
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
return
}
locationsJson = string(locationsJsonBytes)
2021-07-06 19:06:39 +00:00
}
2021-11-13 19:40:25 +00:00
tracksJson := ""
if len(tracks) > 0 {
tracksJsonBytes, err := json.Marshal(tracks)
if err != nil {
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
return
}
tracksJson = string(tracksJsonBytes)
2021-07-06 19:06:39 +00:00
}
a.render(w, r, templateGeoMap, &renderData{
Canonical: canonical,
2021-07-06 19:06:39 +00:00
Data: map[string]interface{}{
2021-11-22 15:36:17 +00:00
"locations": locationsJson,
"tracks": tracksJson,
"attribution": a.getMapAttribution(),
"minzoom": a.getMinZoom(),
"maxzoom": a.getMaxZoom(),
2021-07-06 19:06:39 +00:00
},
})
}