2021-07-06 19:06:39 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2021-07-12 14:19:28 +00:00
|
|
|
const defaultGeoMapPath = "/map"
|
|
|
|
|
2021-07-06 19:06:39 +00:00
|
|
|
func (a *goBlog) serveGeoMap(w http.ResponseWriter, r *http.Request) {
|
2021-12-20 13:00:11 +00:00
|
|
|
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 {
|
2022-01-20 17:22:10 +00:00
|
|
|
a.renderNew(w, r, a.renderGeoMap, &renderData{
|
2021-12-20 13:00:11 +00:00
|
|
|
Canonical: canonical,
|
2022-01-20 17:22:10 +00:00
|
|
|
Data: &geoMapRenderData{
|
|
|
|
noLocations: true,
|
2021-07-06 19:06:39 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type templateLocation struct {
|
|
|
|
Lat float64
|
|
|
|
Lon float64
|
|
|
|
Post string
|
|
|
|
}
|
|
|
|
|
2021-11-13 19:19:46 +00:00
|
|
|
type templateTrack struct {
|
2021-11-16 17:01:11 +00:00
|
|
|
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 {
|
2021-07-31 11:20:51 +00:00
|
|
|
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{
|
2021-11-16 17:01:11 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-01-20 17:22:10 +00:00
|
|
|
a.renderNew(w, r, a.renderGeoMap, &renderData{
|
2021-12-20 13:00:11 +00:00
|
|
|
Canonical: canonical,
|
2022-01-20 17:22:10 +00:00
|
|
|
Data: &geoMapRenderData{
|
|
|
|
locations: locationsJson,
|
|
|
|
tracks: tracksJson,
|
|
|
|
attribution: a.getMapAttribution(),
|
|
|
|
minZoom: a.getMinZoom(),
|
|
|
|
maxZoom: a.getMaxZoom(),
|
2021-07-06 19:06:39 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|