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

View File

@ -3,7 +3,6 @@ package main
import (
"encoding/json"
"errors"
"log"
"math"
"github.com/tkrajina/gpxgo/gpx"
@ -11,8 +10,10 @@ import (
"golang.org/x/text/message"
)
const gpxParameter = "gpx"
func (p *post) HasTrack() bool {
return p.firstParameter("gpx") != ""
return p.firstParameter(gpxParameter) != ""
}
type trackResult struct {
@ -25,7 +26,7 @@ type trackResult struct {
}
func (a *goBlog) getTrack(p *post) (result *trackResult, err error) {
gpxString := p.firstParameter("gpx")
gpxString := p.firstParameter(gpxParameter)
if gpxString == "" {
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
}

View File

@ -272,7 +272,8 @@ type postsRequestConfig struct {
status postStatus
taxonomy *configTaxonomy
taxonomyValue string
parameter string
parameters []string // Ignores parameterValue
parameter string // Ignores parameters
parameterValue string
publishedYear, publishedMonth, publishedDay int
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)")
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 {
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)
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
_, err = app.deletePostFromDb("/test/abc")
must.NoError(err)

View File

@ -1,6 +1,7 @@
(function () {
let mapEl = document.getElementById('map')
let locations = JSON.parse(mapEl.dataset.locations)
let tracks = JSON.parse(mapEl.dataset.tracks)
let map = L.map('map')
@ -8,13 +9,21 @@
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map)
let markers = []
let mapFeatures = []
locations.forEach(loc => {
let marker = [loc.Lat, loc.Lon]
L.marker(marker).addTo(map).on('click', function () {
mapFeatures.push(L.marker([loc.Lat, loc.Lon]).addTo(map).on('click', function () {
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)
let polylines = []
paths.forEach(path => {
let pathPoints = []
path.forEach(point => {
pathPoints.push([point.Lat, point.Lon])
})
let pl = L.polyline(pathPoints, { color: 'blue' }).addTo(map)
polylines.push(pl)
polylines.push(L.polyline(path.map(point => [point.Lat, point.Lon]), { color: 'blue' }).addTo(map))
})
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 }}
<p>{{ string .Blog.Lang "nolocations" }}</p>
{{ 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>
{{ end }}
</main>