@@ 7,12 7,15 @@ import (
"fmt"
"net/http"
"net/url"
+ "regexp"
"strconv"
"strings"
)
var ErrQuota = errors.New("Sorry, the API key seems to have reached its max quota.")
+var locFilterRegexp = regexp.MustCompile(` \([^)]+\)`)
+
type Forecaster struct {
Credentials APICredentials
HTTPClient *http.Client
@@ 153,20 156,29 @@ func (f *Forecaster) Geocode(ctx context.Context, query string) (
b strings.Builder
loc = result.Results[0].Locations[0]
- city = loc.AdminArea5
- state = loc.AdminArea3
- country = loc.AdminArea1
+ city = loc.AdminArea5
+ state = loc.AdminArea3
+ country = loc.AdminArea1
+ locStems = map[string]bool{}
)
+ stemKey := func(s string) string {
+ return locFilterRegexp.ReplaceAllString(strings.ToLower(s), "")
+ }
+
lat = loc.LatLng.Lat
lng = loc.LatLng.Lng
if city != "" {
+ locStems[stemKey(city)] = true
fmt.Fprintf(&b, "%s, ", city)
}
- if state != "" {
+ if state != "" && !locStems[stemKey(state)] {
+ locStems[stemKey(state)] = true
fmt.Fprintf(&b, "%s, ", state)
}
- b.WriteString(country)
+ if !locStems[stemKey(country)] {
+ b.WriteString(country)
+ }
location = b.String()
err = nil
return