Skip to content

Commit

Permalink
Fixing type errors; mapquest currently only sends strings back from i…
Browse files Browse the repository at this point in the history
…ts apis, not float values
  • Loading branch information
kellydunn committed May 5, 2015
1 parent e823924 commit 9d0bf36
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
23 changes: 18 additions & 5 deletions mapquest_geocoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ import (
"io/ioutil"
"net/http"
"net/url"
"strconv"
)

// This struct contains all the funcitonality
// of interacting with the MapQuest Geocoding Service
type MapQuestGeocoder struct{}

type mapQuestGeocodeResponse struct {
BoundingBox []float64 `json:"boundingbox"`
Lat float64
Lng float64 `json:"lon"`
BoundingBox []string `json:"boundingbox"`
Lat string
Lng string `json:"lon"`
DisplayName string `json:"display_name"`
}

Expand Down Expand Up @@ -79,16 +80,28 @@ func (g *MapQuestGeocoder) Geocode(query string) (*Point, error) {
return nil, err
}

fmt.Printf("%s", data)

res := []*mapQuestGeocodeResponse{}
json.Unmarshal(data, &res)

if len(res) == 0 {
return &Point{}, mapquestZeroResultsError
}

lat, err := strconv.ParseFloat(res[0].Lat, 64)
if err != nil {
return nil, err
}

lng, err := strconv.ParseFloat(res[0].Lng, 64)
if err != nil {
return nil, err
}

p := &Point{
lat: res[0].Lat,
lng: res[0].Lng,
lat: lat,
lng: lng,
}

return p, nil
Expand Down
4 changes: 2 additions & 2 deletions mapquest_geocoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestMapQuestGeocodeFromRequest(t *testing.T) {
t.Error("Unexecpected amount of results for mapquest mock response")
}

if res[0].Lat != 37.62181845 || res[0].Lng != -122.383992092462 {
t.Error(fmt.Sprintf("Expected: [37.62181845, -122.383992092462], Got: [%f, %f]", res[0].Lat, res[0].Lng))
if res[0].Lat != "37.62181845" || res[0].Lng != "-122.383992092462" {
t.Error(fmt.Sprintf("Expected: [37.62181845, -122.383992092462], Got: [%s, %s]", res[0].Lat, res[0].Lng))
}
}
24 changes: 12 additions & 12 deletions test/data/mapquest_geocode_success.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
"osm_type": "way",
"osm_id": "23718192",
"boundingbox": [
37.6044343,
37.6392026,
-122.4026019,
-122.3549542
"37.6044343",
"37.6392026",
"-122.4026019",
"-122.3549542"
],
"lat": 37.62181845,
"lon": -122.383992092462,
"lat": "37.62181845",
"lon": "-122.383992092462",
"display_name": "San Francisco International Airport, Walkway thru SFO Terminals, Millbrae, San Mateo County, California, 94128, United States of America",
"class": "aeroway",
"type": "aerodrome",
Expand All @@ -23,13 +23,13 @@
"osm_type": "node",
"osm_id": "1042081309",
"boundingbox": [
-12.53305,
-12.53295,
-73.8000531,
-73.7999531
"-12.53305",
"-12.53295",
"-73.8000531",
"-73.7999531"
],
"lat": -12.533,
"lon": -73.8000031,
"lat": "-12.533",
"lon": "-73.8000031",
"display_name": "San Francisco Airport, Pichari, La Convención, Department of Cusco, Peru",
"class": "aeroway",
"type": "aerodrome",
Expand Down

0 comments on commit 9d0bf36

Please sign in to comment.