Skip to content

Commit

Permalink
MapQuest Nominatim service requires a key now
Browse files Browse the repository at this point in the history
code clean up
  • Loading branch information
codingsince1985 committed Apr 2, 2016
1 parent b71add7 commit 10535c7
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 64 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func main() {
try(google.Geocoder())

// MapQuest Nominatim
try(nominatim.Geocoder())
try(nominatim.Geocoder("MAPQUEST_KEY"))

// MapQuest Open
try(open.Geocoder("MAPQUEST_KEY"))
Expand Down
9 changes: 3 additions & 6 deletions bing/geocoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,19 @@ const key = "YOUR_KEY"
var geocoder = bing.Geocoder(key)

func TestGeocode(t *testing.T) {
location, err := geocoder.Geocode("Melbourne VIC")
if err != nil || location.Lat != -37.82429885864258 || location.Lng != 144.97799682617188 {
if location, err := geocoder.Geocode("Melbourne VIC"); err != nil || location.Lat != -37.82429885864258 || location.Lng != 144.97799682617188 {
t.Error("TestGeocode() failed", err, location)
}
}

func TestReverseGeocode(t *testing.T) {
address, err := geocoder.ReverseGeocode(-37.816742, 144.964463)
if err != nil || !strings.HasSuffix(address, "Melbourne, VIC 3000") {
if address, err := geocoder.ReverseGeocode(-37.816742, 144.964463); err != nil || !strings.HasSuffix(address, "Melbourne, VIC 3000") {
t.Error("TestReverseGeocode() failed", err, address)
}
}

func TestReverseGeocodeWithNoResult(t *testing.T) {
_, err := geocoder.ReverseGeocode(-37.816742, 164.964463)
if err != geo.ErrNoResult {
if _, err := geocoder.ReverseGeocode(-37.816742, 164.964463); err != geo.ErrNoResult {
t.Error("TestReverseGeocodeWithNoResult() failed", err)
}
}
7 changes: 2 additions & 5 deletions google/geocoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ type geocodeResponse struct {
Results []struct {
Formatted_Address string
Geometry struct {
Location struct {
Lat, Lng float64
}
Location geo.Location
}
}
}
Expand All @@ -37,8 +35,7 @@ func (b baseURL) ReverseGeocodeURL(l geo.Location) string {

func (r *geocodeResponse) Location() (l geo.Location) {
if len(r.Results) > 0 {
loc := r.Results[0].Geometry.Location
l = geo.Location{loc.Lat, loc.Lng}
l = r.Results[0].Geometry.Location
}
return
}
Expand Down
9 changes: 3 additions & 6 deletions google/geocoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,19 @@ import (
var geocoder = google.Geocoder()

func TestGeocode(t *testing.T) {
location, err := geocoder.Geocode("Melbourne VIC")
if err != nil || location.Lat != -37.814107 || location.Lng != 144.96328 {
if location, err := geocoder.Geocode("Melbourne VIC"); err != nil || location.Lat != -37.814107 || location.Lng != 144.96328 {
t.Error("TestGeocode() failed", err, location)
}
}

func TestReverseGeocode(t *testing.T) {
address, err := geocoder.ReverseGeocode(-37.816742, 144.964463)
if err != nil || !strings.HasSuffix(address, "Melbourne VIC 3000, Australia") {
if address, err := geocoder.ReverseGeocode(-37.816742, 144.964463); err != nil || !strings.HasSuffix(address, "Melbourne VIC 3000, Australia") {
t.Error("TestReverseGeocode() failed", err, address)
}
}

func TestReverseGeocodeWithNoResult(t *testing.T) {
_, err := geocoder.ReverseGeocode(-37.816742, 164.964463)
if err != geo.ErrNoResult {
if _, err := geocoder.ReverseGeocode(-37.816742, 164.964463); err != geo.ErrNoResult {
t.Error("TestReverseGeocodeWithNoResult() failed", err)
}
}
9 changes: 3 additions & 6 deletions here/geocoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,19 @@ const appCode = "YOUR_APP_CODE"
var geocoder = here.Geocoder(appID, appCode, 100)

func TestGeocode(t *testing.T) {
location, err := geocoder.Geocode("Melbourne VIC")
if err != nil || location.Lat != -37.81753 || location.Lng != 144.96715 {
if location, err := geocoder.Geocode("Melbourne VIC"); err != nil || location.Lat != -37.81753 || location.Lng != 144.96715 {
t.Error("TestGeocode() failed", err, location)
}
}

func TestReverseGeocode(t *testing.T) {
address, err := geocoder.ReverseGeocode(-37.816742, 144.964463)
if err != nil || !strings.HasSuffix(address, "VIC 3000, Australia") {
if address, err := geocoder.ReverseGeocode(-37.816742, 144.964463); err != nil || !strings.HasSuffix(address, "VIC 3000, Australia") {
t.Error("TestReverseGeocode() failed", err, address)
}
}

func TestReverseGeocodeWithNoResult(t *testing.T) {
_, err := geocoder.ReverseGeocode(-37.816742, 164.964463)
if err != geo.ErrNoResult {
if _, err := geocoder.ReverseGeocode(-37.816742, 164.964463); err != geo.ErrNoResult {
t.Error("TestReverseGeocodeWithNoResult() failed", err)
}
}
3 changes: 1 addition & 2 deletions mapbox/geocoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import (
type baseURL string

type geocodeResponse struct {
Query []interface{}
Features []struct {
Place_Name string
Center [2]float64
Center [2]float64
}
}

Expand Down
9 changes: 3 additions & 6 deletions mapbox/geocoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,19 @@ const token = "YOUR_ACCESS_TOKEN"
var geocoder = mapbox.Geocoder(token)

func TestGeocode(t *testing.T) {
location, err := geocoder.Geocode("Melbourne VIC")
if err != nil || location.Lat != -37.8142 || location.Lng != 144.9632 {
if location, err := geocoder.Geocode("Melbourne VIC"); err != nil || location.Lat != -37.8142 || location.Lng != 144.9632 {
t.Error("TestGeocode() failed", err, location)
}
}

func TestReverseGeocode(t *testing.T) {
address, err := geocoder.ReverseGeocode(-37.8142, 144.9632)
if err != nil || !strings.HasSuffix(address, "Melbourne, Victoria 3000, Australia") {
if address, err := geocoder.ReverseGeocode(-37.8142, 144.9632); err != nil || !strings.HasSuffix(address, "Melbourne, Victoria 3000, Australia") {
t.Error("TestReverseGeocode() failed", err, address)
}
}

func TestReverseGeocodeWithNoResult(t *testing.T) {
_, err := geocoder.ReverseGeocode(-37.8142, 164.9632)
if err != geo.ErrNoResult {
if _, err := geocoder.ReverseGeocode(-37.8142, 164.9632); err != geo.ErrNoResult {
t.Error("TestReverseGeocodeWithNoResult() failed", err)
}
}
9 changes: 6 additions & 3 deletions mapquest/nominatim/geocoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,23 @@ type geocodeResponse struct {
Display_Name, Lat, Lon, Error string
}

var key string

// Geocoder constructs MapRequest Nominatim geocoder
func Geocoder() geo.Geocoder {
func Geocoder(k string) geo.Geocoder {
key = k
return geo.Geocoder{
baseURL("http://open.mapquestapi.com/nominatim/v1/"),
&geocodeResponse{},
}
}

func (b baseURL) GeocodeURL(address string) string {
return string(b) + "search.php?format=json&limit=1&q=" + address
return string(b) + "search.php?key=" + key + "&format=json&limit=1&q=" + address
}

func (b baseURL) ReverseGeocodeURL(l geo.Location) string {
return string(b) + fmt.Sprintf("reverse.php?format=json&lat=%f&lon=%f", l.Lat, l.Lng)
return string(b) + "reverse.php?key=" + key + fmt.Sprintf("&format=json&lat=%f&lon=%f", l.Lat, l.Lng)
}

func (r *geocodeResponse) Location() (l geo.Location) {
Expand Down
13 changes: 6 additions & 7 deletions mapquest/nominatim/geocoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,24 @@ import (
"testing"
)

var geocoder = nominatim.Geocoder()
const key = "YOUR_KEY"

var geocoder = nominatim.Geocoder(key)

func TestGeocode(t *testing.T) {
location, err := geocoder.Geocode("Melbourne VIC")
if err != nil || location.Lat != -37.8142176 || location.Lng != 144.9631608 {
if location, err := geocoder.Geocode("Melbourne VIC"); err != nil || location.Lat != -37.8142176 || location.Lng != 144.9631608 {
t.Error("TestGeocode() failed", err, location)
}
}

func TestReverseGeocode(t *testing.T) {
address, err := geocoder.ReverseGeocode(-37.816742, 144.964463)
if err != nil || !strings.HasSuffix(address, "Melbourne, Victoria, 3000, Australia") {
if address, err := geocoder.ReverseGeocode(-37.816742, 144.964463); err != nil || !strings.HasSuffix(address, "Melbourne, Victoria, 3000, Australia") {
t.Error("TestReverseGeocode() failed", err, address)
}
}

func TestReverseGeocodeWithNoResult(t *testing.T) {
_, err := geocoder.ReverseGeocode(-37.816742, 164.964463)
if err != geo.ErrNoResult {
if _, err := geocoder.ReverseGeocode(-37.816742, 164.964463); err != geo.ErrNoResult {
t.Error("TestReverseGeocodeWithNoResult() failed", err)
}
}
7 changes: 2 additions & 5 deletions mapquest/open/geocoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ type baseURL string
type geocodeResponse struct {
Results []struct {
Locations []struct {
LatLng struct {
Lat, Lng float64
}
LatLng geo.Location
Street, AdminArea5, AdminArea3, AdminArea1 string
}
}
Expand All @@ -37,8 +35,7 @@ func (b baseURL) ReverseGeocodeURL(l geo.Location) string {
}

func (r *geocodeResponse) Location() geo.Location {
p := r.Results[0].Locations[0].LatLng
return geo.Location{p.Lat, p.Lng}
return r.Results[0].Locations[0].LatLng
}

func (r *geocodeResponse) Address() (address string) {
Expand Down
9 changes: 3 additions & 6 deletions mapquest/open/geocoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,19 @@ const key = "YOUR_KEY"
var geocoder = open.Geocoder(key)

func TestGeocode(t *testing.T) {
location, err := geocoder.Geocode("Melbourne VIC")
if err != nil || location.Lat != -37.814218 || location.Lng != 144.963161 {
if location, err := geocoder.Geocode("Melbourne VIC"); err != nil || location.Lat != -37.814218 || location.Lng != 144.963161 {
t.Error("TestGeocode() failed", err, location)
}
}

func TestReverseGeocode(t *testing.T) {
address, err := geocoder.ReverseGeocode(-37.816742, 144.964463)
if err != nil || !strings.HasSuffix(address, "Melbourne, Victoria, AU") {
if address, err := geocoder.ReverseGeocode(-37.816742, 144.964463); err != nil || !strings.HasSuffix(address, "Melbourne, Victoria, AU") {
t.Error("TestReverseGeocode() failed", err, address)
}
}

func TestReverseGeocodeWithNoResult(t *testing.T) {
_, err := geocoder.ReverseGeocode(-37.816742, 164.964463)
if err != geo.ErrNoResult {
if _, err := geocoder.ReverseGeocode(-37.816742, 164.964463); err != geo.ErrNoResult {
t.Error("TestReverseGeocodeWithNoResult() failed", err)
}
}
7 changes: 2 additions & 5 deletions opencage/geocoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ type baseURL string
type geocodeResponse struct {
Results []struct {
Formatted string
Geometry struct {
Lat, Lng float64
}
Geometry geo.Location
}
}

Expand All @@ -35,8 +33,7 @@ func (b baseURL) ReverseGeocodeURL(l geo.Location) string {

func (r *geocodeResponse) Location() (l geo.Location) {
if len(r.Results) > 0 {
g := r.Results[0].Geometry
l = geo.Location{g.Lat, g.Lng}
l = r.Results[0].Geometry
}
return
}
Expand Down
9 changes: 3 additions & 6 deletions opencage/geocoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,19 @@ const key = "YOUR_KEY"
var geocoder = opencage.Geocoder(key)

func TestGeocode(t *testing.T) {
location, err := geocoder.Geocode("Melbourne VIC")
if err != nil || location.Lat != -37.8142176 || location.Lng != 144.9631608 {
if location, err := geocoder.Geocode("Melbourne VIC"); err != nil || location.Lat != -37.8142176 || location.Lng != 144.9631608 {
t.Error("TestGeocode() failed", err, location)
}
}

func TestReverseGeocode(t *testing.T) {
address, err := geocoder.ReverseGeocode(-37.816742, 144.964463)
if err != nil || !strings.HasSuffix(address, "Melbourne VIC 3000, Australia") {
if address, err := geocoder.ReverseGeocode(-37.816742, 144.964463); err != nil || !strings.HasSuffix(address, "Melbourne VIC 3000, Australia") {
t.Error("TestReverseGeocode() failed", err, address)
}
}

func TestReverseGeocodeWithNoResult(t *testing.T) {
_, err := geocoder.ReverseGeocode(-37.816742, 164.964463)
if err != geo.ErrNoResult {
if _, err := geocoder.ReverseGeocode(-37.816742, 164.964463); err != geo.ErrNoResult {
t.Error("TestReverseGeocodeWithNoResult() failed", err)
}
}

0 comments on commit 10535c7

Please sign in to comment.