Show tracks on map as well

This commit is contained in:
Jan-Lukas Else 2021-11-13 20:19:46 +01:00
parent 2c6cd401bc
commit 0ea54f87a6
7 changed files with 66 additions and 25 deletions

View File

@ -14,8 +14,8 @@ func (a *goBlog) serveGeoMap(w http.ResponseWriter, r *http.Request) {
allPostsWithLocation, err := a.getPosts(&postsRequestConfig{ allPostsWithLocation, err := a.getPosts(&postsRequestConfig{
blog: blog, blog: blog,
status: statusPublished, status: statusPublished,
parameter: a.cfg.Micropub.LocationParam, parameters: []string{a.cfg.Micropub.LocationParam, gpxParameter},
withOnlyParameters: []string{a.cfg.Micropub.LocationParam}, withOnlyParameters: []string{a.cfg.Micropub.LocationParam, gpxParameter},
}) })
if err != nil { if err != nil {
a.serveError(w, r, err.Error(), http.StatusInternalServerError) a.serveError(w, r, err.Error(), http.StatusInternalServerError)
@ -38,7 +38,13 @@ func (a *goBlog) serveGeoMap(w http.ResponseWriter, r *http.Request) {
Post string Post string
} }
type templateTrack struct {
Paths [][]*trackPoint
Post string
}
var locations []*templateLocation var locations []*templateLocation
var tracks []*templateTrack
for _, p := range allPostsWithLocation { for _, p := range allPostsWithLocation {
if g := a.geoURI(p); g != nil { if g := a.geoURI(p); g != nil {
locations = append(locations, &templateLocation{ locations = append(locations, &templateLocation{
@ -47,9 +53,21 @@ func (a *goBlog) serveGeoMap(w http.ResponseWriter, r *http.Request) {
Post: p.Path, Post: p.Path,
}) })
} }
if t, err := a.getTrack(p); err == nil && t != nil {
tracks = append(tracks, &templateTrack{
Paths: t.Paths,
Post: p.Path,
})
}
} }
jb, err := json.Marshal(locations) locationsJsonBytes, err := json.Marshal(locations)
if err != nil {
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
return
}
tracksJsonBytes, err := json.Marshal(tracks)
if err != nil { if err != nil {
a.serveError(w, r, err.Error(), http.StatusInternalServerError) a.serveError(w, r, err.Error(), http.StatusInternalServerError)
return return
@ -60,7 +78,8 @@ func (a *goBlog) serveGeoMap(w http.ResponseWriter, r *http.Request) {
BlogString: blog, BlogString: blog,
Canonical: a.getFullAddress(mapPath), Canonical: a.getFullAddress(mapPath),
Data: map[string]interface{}{ Data: map[string]interface{}{
"locations": string(jb), "locations": string(locationsJsonBytes),
"tracks": string(tracksJsonBytes),
}, },
}) })
} }

View File

@ -3,7 +3,6 @@ package main
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"log"
"math" "math"
"github.com/tkrajina/gpxgo/gpx" "github.com/tkrajina/gpxgo/gpx"
@ -11,8 +10,10 @@ import (
"golang.org/x/text/message" "golang.org/x/text/message"
) )
const gpxParameter = "gpx"
func (p *post) HasTrack() bool { func (p *post) HasTrack() bool {
return p.firstParameter("gpx") != "" return p.firstParameter(gpxParameter) != ""
} }
type trackResult struct { type trackResult struct {
@ -25,7 +26,7 @@ type trackResult struct {
} }
func (a *goBlog) getTrack(p *post) (result *trackResult, err error) { func (a *goBlog) getTrack(p *post) (result *trackResult, err error) {
gpxString := p.firstParameter("gpx") gpxString := p.firstParameter(gpxParameter)
if gpxString == "" { if gpxString == "" {
return nil, errors.New("no gpx parameter in post") return nil, errors.New("no gpx parameter in post")
} }
@ -58,8 +59,6 @@ func (a *goBlog) getTrack(p *post) (result *trackResult, err error) {
), ),
} }
log.Println(string(pathsJSON))
return result, nil return result, nil
} }

View File

@ -272,7 +272,8 @@ type postsRequestConfig struct {
status postStatus status postStatus
taxonomy *configTaxonomy taxonomy *configTaxonomy
taxonomyValue string taxonomyValue string
parameter string parameters []string // Ignores parameterValue
parameter string // Ignores parameters
parameterValue string parameterValue string
publishedYear, publishedMonth, publishedDay int publishedYear, publishedMonth, publishedDay int
randomOrder bool randomOrder bool
@ -317,6 +318,18 @@ func buildPostsQuery(c *postsRequestConfig, selection string) (query string, arg
queryBuilder.WriteString(" and path in (select path from post_parameters where parameter = @param and length(coalesce(value, '')) > 0)") queryBuilder.WriteString(" and path in (select path from post_parameters where parameter = @param and length(coalesce(value, '')) > 0)")
args = append(args, sql.Named("param", c.parameter)) args = append(args, sql.Named("param", c.parameter))
} }
} else if len(c.parameters) > 0 {
queryBuilder.WriteString(" and path in (select path from post_parameters where parameter in (")
for i, param := range c.parameters {
if i > 0 {
queryBuilder.WriteString(", ")
}
named := "param" + strconv.Itoa(i)
queryBuilder.WriteByte('@')
queryBuilder.WriteString(named)
args = append(args, param)
}
queryBuilder.WriteString(") and length(coalesce(value, '')) > 0)")
} }
if c.taxonomy != nil && len(c.taxonomyValue) > 0 { if c.taxonomy != nil && len(c.taxonomyValue) > 0 {
queryBuilder.WriteString(" and path in (select path from post_parameters where parameter = @taxname and lowerx(value) = lowerx(@taxval))") queryBuilder.WriteString(" and path in (select path from post_parameters where parameter = @taxname and lowerx(value) = lowerx(@taxval))")

View File

@ -90,6 +90,11 @@ func Test_postsDb(t *testing.T) {
must.NoError(err) must.NoError(err)
is.Equal(0, count) is.Equal(0, count)
// Check by multiple parameters
count, err = app.db.countPosts(&postsRequestConfig{parameters: []string{"tags", "title"}})
must.NoError(err)
is.Equal(1, count)
// Delete post // Delete post
_, err = app.deletePostFromDb("/test/abc") _, err = app.deletePostFromDb("/test/abc")
must.NoError(err) must.NoError(err)

View File

@ -1,6 +1,7 @@
(function () { (function () {
let mapEl = document.getElementById('map') let mapEl = document.getElementById('map')
let locations = JSON.parse(mapEl.dataset.locations) let locations = JSON.parse(mapEl.dataset.locations)
let tracks = JSON.parse(mapEl.dataset.tracks)
let map = L.map('map') let map = L.map('map')
@ -8,13 +9,21 @@
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors' attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map) }).addTo(map)
let markers = [] let mapFeatures = []
locations.forEach(loc => { locations.forEach(loc => {
let marker = [loc.Lat, loc.Lon] mapFeatures.push(L.marker([loc.Lat, loc.Lon]).addTo(map).on('click', function () {
L.marker(marker).addTo(map).on('click', function () {
window.open(loc.Post, '_blank').focus() window.open(loc.Post, '_blank').focus()
}) }))
markers.push(marker)
}) })
map.fitBounds(markers, { padding: [5, 5] })
tracks.forEach(track => {
track.Paths.forEach(path => {
mapFeatures.push(L.polyline(path.map(point => [point.Lat, point.Lon]), { color: 'blue' }).addTo(map).on('click', function () {
window.open(track.Post, '_blank').focus()
}))
})
})
map.fitBounds(L.featureGroup(mapFeatures).getBounds(), { padding: [5, 5] })
})() })()

View File

@ -9,14 +9,10 @@
}).addTo(map) }).addTo(map)
let polylines = [] let polylines = []
paths.forEach(path => { paths.forEach(path => {
let pathPoints = [] polylines.push(L.polyline(path.map(point => [point.Lat, point.Lon]), { color: 'blue' }).addTo(map))
path.forEach(point => {
pathPoints.push([point.Lat, point.Lon])
})
let pl = L.polyline(pathPoints, { color: 'blue' }).addTo(map)
polylines.push(pl)
}) })
let fgb = L.featureGroup(polylines).getBounds()
map.fitBounds(fgb, { padding: [5, 5] }) map.fitBounds(L.featureGroup(polylines).getBounds(), { padding: [5, 5] })
})() })()

View File

@ -11,7 +11,7 @@
{{ if .Data.nolocations }} {{ if .Data.nolocations }}
<p>{{ string .Blog.Lang "nolocations" }}</p> <p>{{ string .Blog.Lang "nolocations" }}</p>
{{ else }} {{ else }}
<div class="p" id="map" data-locations="{{ .Data.locations }}"></div> <div class="p" id="map" data-locations="{{ .Data.locations }}" data-tracks="{{ .Data.tracks }}"></div>
<script defer src="{{ asset "js/geomap.js" }}"></script> <script defer src="{{ asset "js/geomap.js" }}"></script>
{{ end }} {{ end }}
</main> </main>