This commit is contained in:
Martin Donnelly 2024-02-26 11:32:39 +00:00
commit 4a5c65dd1a
4 changed files with 90 additions and 0 deletions

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module git.caliban.io/martin/go-geocode
go 1.22.0
require github.com/codingsince1985/geo-golang v1.8.3

10
go.sum Normal file
View File

@ -0,0 +1,10 @@
github.com/codingsince1985/geo-golang v1.8.3 h1:73TRG/poj1IUiYOoaEM7gD/+ZBSRg+BPnWoGpAg+NHc=
github.com/codingsince1985/geo-golang v1.8.3/go.mod h1:IQXA9sjsQ1hTJfijQcsQInvnzdn7B0rx+VTNDLpaqiw=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

49
main.go Normal file
View File

@ -0,0 +1,49 @@
package go_geocode
import (
"log"
"github.com/codingsince1985/geo-golang"
"github.com/codingsince1985/geo-golang/openstreetmap"
)
func Geocode(latlong LatLong) *FormattedLocation {
return tryGeocoder(openstreetmap.Geocoder(), latlong)
}
func tryGeocoder(geocoder geo.Geocoder, latlong LatLong) *FormattedLocation {
address, _ := geocoder.ReverseGeocode(latlong.Lat, latlong.Long)
if address != nil {
log.Printf("%+v\n", address)
return reformatGeocode(*address, latlong)
} else {
return new(FormattedLocation)
}
}
func reformatGeocode(address geo.Address, latlong LatLong) *FormattedLocation{
return &FormattedLocation{
Lat: latlong.Lat,
Long: latlong.Long,
Country: address.Country,
City: address.City,
State: address.State,
Zipcode: address.Postcode,
StreetName: address.Street,
CountryCode: address.CountryCode,
County: address.County,
Neighbourhood: address.Suburb,
Village: "",
Formatted: address.FormattedAddress,
}
}

26
structs.go Normal file
View File

@ -0,0 +1,26 @@
package go_geocode
type LL struct {
Ll string `json:"ll" form:"ll"`
}
type LatLong struct {
Lat float64 `json:"lat" form:"lat"`
Long float64 `json:"long" form:"long"`
}
type FormattedLocation struct {
Lat float64 `json:"lat" form:"lat"`
Long float64 `json:"long" form:"long"`
Country string `json:"country"`
City string `json:"city"`
State string `json:"state"`
Zipcode string `json:"zipcode"`
StreetName string `json:"streetName"`
CountryCode string `json:"countryCode"`
County string `json:"county"`
Neighbourhood string `json:"neighbourhood"`
Village string `json:"village"`
Formatted string `json:"formatted"`
}