50 lines
979 B
Go
50 lines
979 B
Go
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,
|
|
|
|
}
|
|
|
|
}
|