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-07-27 10:51:08 +00:00
|
|
|
blog := r.Context().Value(blogKey).(string)
|
2021-07-12 14:19:28 +00:00
|
|
|
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,
|
2021-07-31 11:20:51 +00:00
|
|
|
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 {
|
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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|