GoBlog/geoMap.go

67 lines
1.4 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 := r.Context().Value(blogKey).(string)
bc := a.cfg.Blogs[blog]
2021-07-06 19:06:39 +00:00
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,
parameter: a.cfg.Micropub.LocationParam,
withOnlyParameters: []string{a.cfg.Micropub.LocationParam},
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{
BlogString: blog,
Data: map[string]interface{}{
"nolocations": true,
},
})
return
}
type templateLocation struct {
Lat float64
Lon float64
Post string
}
var locations []*templateLocation
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,
})
}
}
jb, err := json.Marshal(locations)
if err != nil {
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
return
}
2021-07-19 16:41:38 +00:00
mapPath := bc.getRelativePath(defaultIfEmpty(bc.Map.Path, defaultGeoMapPath))
2021-07-06 19:06:39 +00:00
a.render(w, r, templateGeoMap, &renderData{
BlogString: blog,
2021-07-19 16:41:38 +00:00
Canonical: a.getFullAddress(mapPath),
2021-07-06 19:06:39 +00:00
Data: map[string]interface{}{
"locations": string(jb),
},
})
}