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"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
geojson "github.com/paulmach/go.geojson"
|
geojson "github.com/paulmach/go.geojson"
|
||||||
"github.com/thoas/go-funk"
|
"github.com/thoas/go-funk"
|
||||||
)
|
)
|
||||||
|
|
||||||
func geoTitle(lat, lon float64) string {
|
func geoTitle(lat, lon float64, lang string) string {
|
||||||
ba, err := photonReverse(lat, lon)
|
ba, err := photonReverse(lat, lon, lang)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ""
|
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 != "" }), ", ")
|
return strings.Join(funk.FilterString([]string{name, city, state, country}, func(s string) bool { return s != "" }), ", ")
|
||||||
}
|
}
|
||||||
|
|
||||||
func photonReverse(lat, lon float64) ([]byte, error) {
|
func photonReverse(lat, lon float64, lang string) ([]byte, error) {
|
||||||
cacheKey := fmt.Sprintf("photon-%v-%v", lat, lon)
|
cacheKey := fmt.Sprintf("photon-%v-%v-%v", lat, lon, lang)
|
||||||
cache, _ := retrievePersistentCache(cacheKey)
|
cache, _ := retrievePersistentCache(cacheKey)
|
||||||
if cache != nil {
|
if cache != nil {
|
||||||
return 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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
{{ define "postmeta" }}
|
{{ define "postmeta" }}
|
||||||
<div class="p">
|
<div class="p">
|
||||||
{{ include "summaryandpostmeta" . }}
|
{{ include "summaryandpostmeta" . }}
|
||||||
|
{{ $bloglang := .Blog.Lang }}
|
||||||
{{ $loc := ( p .Data "location" ) }}
|
{{ $loc := ( p .Data "location" ) }}
|
||||||
{{ if $loc }}
|
{{ if $loc }}
|
||||||
{{ with geouri $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">
|
<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-longitude" value="{{ .Longitude }}" />
|
||||||
<data class="p-latitude" value="{{ .Latitude }}" />
|
<data class="p-latitude" value="{{ .Latitude }}" />
|
||||||
</a></div>
|
</a></div>
|
||||||
|
|
Loading…
Reference in New Issue