mirror of https://github.com/jlelse/GoBlog
Geo title based on blog language
This commit is contained in:
parent
d273280bf9
commit
0219a6302b
|
@ -5,14 +5,15 @@ import (
|
|||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
geojson "github.com/paulmach/go.geojson"
|
||||
"github.com/thoas/go-funk"
|
||||
)
|
||||
|
||||
func geoTitle(lat, lon float64) string {
|
||||
ba, err := photonReverse(lat, lon)
|
||||
func geoTitle(lat, lon float64, lang string) string {
|
||||
ba, err := photonReverse(lat, lon, lang)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
@ -28,13 +29,21 @@ func geoTitle(lat, lon float64) string {
|
|||
return strings.Join(funk.FilterString([]string{name, city, state, country}, func(s string) bool { return s != "" }), ", ")
|
||||
}
|
||||
|
||||
func photonReverse(lat, lon float64) ([]byte, error) {
|
||||
cacheKey := fmt.Sprintf("photon-%v-%v", lat, lon)
|
||||
func photonReverse(lat, lon float64, lang string) ([]byte, error) {
|
||||
cacheKey := fmt.Sprintf("photon-%v-%v-%v", lat, lon, lang)
|
||||
cache, _ := retrievePersistentCache(cacheKey)
|
||||
if cache != nil {
|
||||
return cache, nil
|
||||
}
|
||||
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://photon.komoot.io/reverse?lat=%v&lon=%v", lat, lon), nil)
|
||||
uv := url.Values{}
|
||||
uv.Set("lat", fmt.Sprintf("%v", lat))
|
||||
uv.Set("lon", fmt.Sprintf("%v", lon))
|
||||
if lang == "de" || lang == "fr" || lang == "it" {
|
||||
uv.Set("lang", lang)
|
||||
} else {
|
||||
uv.Set("lang", "en")
|
||||
}
|
||||
req, err := http.NewRequest(http.MethodGet, "https://photon.komoot.io/reverse?"+uv.Encode(), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
{{ define "postmeta" }}
|
||||
<div class="p">
|
||||
{{ include "summaryandpostmeta" . }}
|
||||
{{ $bloglang := .Blog.Lang }}
|
||||
{{ $loc := ( p .Data "location" ) }}
|
||||
{{ if $loc }}
|
||||
{{ with geouri $loc }}
|
||||
<div>📍 <a class="p-location h-geo" href="https://www.openstreetmap.org/?mlat={{ .Latitude }}&mlon={{ .Longitude }}" target="_blank" rel="nofollow noopener noreferrer">
|
||||
<span class="p-name">{{ with ( geourip . "name" ) }}{{ . }}{{ else }}{{ with ( geotitle .Latitude .Longitude ) }}{{ . }}{{ else }}{{ .Latitude }}, {{ .Longitude }}{{ end }}{{ end }}</span>
|
||||
<span class="p-name">{{ with ( geourip . "name" ) }}{{ . }}{{ else }}{{ with ( geotitle .Latitude .Longitude $bloglang ) }}{{ . }}{{ else }}{{ .Latitude }}, {{ .Longitude }}{{ end }}{{ end }}</span>
|
||||
<data class="p-longitude" value="{{ .Longitude }}" />
|
||||
<data class="p-latitude" value="{{ .Latitude }}" />
|
||||
</a></div>
|
||||
|
|
Loading…
Reference in New Issue