diff --git a/addressdescriptor.go b/addressdescriptor.go index 3deb17c..6b4d966 100644 --- a/addressdescriptor.go +++ b/addressdescriptor.go @@ -14,6 +14,10 @@ package maps +import ( + "fmt" +) + /** * An enum representing the relationship in space between the landmark and the target. */ @@ -22,23 +26,23 @@ type SpatialRelationship string const ( // This is the default relationship when nothing more specific below // applies. - NEAR SpatialRelationship = "NEAR" + SPATIAL_RELATIONSHIP_NEAR SpatialRelationship = "NEAR" // The landmark has a spatial geometry and the target is within its // bounds. - WITHIN SpatialRelationship = "WITHIN" + SPATIAL_RELATIONSHIP_WITHIN SpatialRelationship = "WITHIN" // The target is directly adjacent to the landmark or landmark's access // point. - BESIDE SpatialRelationship = "BESIDE" + SPATIAL_RELATIONSHIP_BESIDE SpatialRelationship = "BESIDE" // The target is directly opposite the landmark on the other side of the // road. - ACROSS_THE_ROAD SpatialRelationship = "ACROSS_THE_ROAD" + SPATIAL_RELATIONSHIP_ACROSS_THE_ROAD SpatialRelationship = "ACROSS_THE_ROAD" // On the same route as the landmark but not besides or across. - DOWN_THE_ROAD SpatialRelationship = "DOWN_THE_ROAD" + SPATIAL_RELATIONSHIP_DOWN_THE_ROAD SpatialRelationship = "DOWN_THE_ROAD" // Not on the same route as the landmark but a single 'turn' away. - AROUND_THE_CORNER SpatialRelationship = "AROUND_THE_CORNER" + SPATIAL_RELATIONSHIP_AROUND_THE_CORNER SpatialRelationship = "AROUND_THE_CORNER" // Close to the landmark's structure but further away from its access // point. - BEHIND SpatialRelationship = "BEHIND" + SPATIAL_RELATIONSHIP_BEHIND SpatialRelationship = "BEHIND" ) // String method for formatted output @@ -57,11 +61,11 @@ const ( */ CONTAINMENT_UNSPECIFIED Containment = "CONTAINMENT_UNSPECIFIED" /** The target location is within the area region, close to the center. */ - WITHIN Containment = "WITHIN" + CONTAINMENT_WITHIN Containment = "WITHIN" /** The target location is within the area region, close to the edge. */ - OUTSKIRTS Containment = "OUTSKIRTS" + CONTAINMENT_OUTSKIRTS Containment = "OUTSKIRTS" /** The target location is outside the area region, but close by. */ - NEAR Containment = "NEAR" + CONTAINMENT_NEAR Containment = "NEAR" ) // String method for formatted output @@ -137,7 +141,7 @@ type Area struct { type AddressDescriptor struct { // A ranked list of nearby landmarks. The most useful (recognizable and // nearby) landmarks are ranked first. - Landmarks []Landmark `json:"landmarks" + Landmarks []Landmark `json:"landmarks"` // A ranked list of containing or adjacent areas. The most useful // (recognizable and precise) areas are ranked first. Areas []Area `json:"areas"` diff --git a/geocoding.go b/geocoding.go index 35d174b..cdf0dd1 100644 --- a/geocoding.go +++ b/geocoding.go @@ -34,7 +34,7 @@ var geocodingAPI = &apiConfig{ // Geocode makes a Geocoding API request func (c *Client) Geocode(ctx context.Context, r *GeocodingRequest) (GeocodingResponse, error) { if r.Address == "" && len(r.Components) == 0 && r.LatLng == nil { - return nil, errors.New("maps: address, components and LatLng are all missing") + return GeocodingResponse{}, errors.New("maps: address, components and LatLng are all missing") } var response struct { @@ -50,14 +50,14 @@ func (c *Client) Geocode(ctx context.Context, r *GeocodingRequest) (GeocodingRes return GeocodingResponse{}, err } - return GeocodingResponse{response.Results},, nil + return GeocodingResponse{response.Results, AddressDescriptor{}}, nil } // ReverseGeocode makes a Reverse Geocoding API request func (c *Client) ReverseGeocode(ctx context.Context, r *GeocodingRequest) (GeocodingResponse, error) { // Since Geocode() does not allow a nil LatLng, whereas it is allowed here if r.LatLng == nil && r.PlaceID == "" { - return nil, errors.New("maps: LatLng and PlaceID are both missing") + return GeocodingResponse{}, errors.New("maps: LatLng and PlaceID are both missing") } var response struct { diff --git a/geocoding_test.go b/geocoding_test.go index 579acde..56d76f3 100644 --- a/geocoding_test.go +++ b/geocoding_test.go @@ -127,69 +127,71 @@ func TestGeocodingGoogleHQ(t *testing.T) { resp, err := c.Geocode(context.Background(), r) - if len(resp) != 1 { - t.Errorf("Expected length of response is 1, was %+v", len(resp)) + if len(resp.Results) != 1 { + t.Errorf("Expected length of response is 1, was %+v", len(resp.Results)) } if err != nil { t.Errorf("r.Get returned non nil error: %v", err) } correctResponse := GeocodingResponse{ - Results: GeocodingResult{ - AddressComponents: []AddressComponent{ - { - LongName: "1600", - ShortName: "1600", - Types: []string{"street_number"}, - }, - { - LongName: "Amphitheatre Pkwy", - ShortName: "Amphitheatre Pkwy", - Types: []string{"route"}, - }, - { - LongName: "Mountain View", - ShortName: "Mountain View", - Types: []string{"locality", "political"}, - }, - { - LongName: "Santa Clara County", - ShortName: "Santa Clara County", - Types: []string{"administrative_area_level_2", "political"}, - }, - { - LongName: "California", - ShortName: "CA", - Types: []string{"administrative_area_level_1", "political"}, - }, - { - LongName: "United States", - ShortName: "US", - Types: []string{"country", "political"}, - }, - { - LongName: "94043", - ShortName: "94043", - Types: []string{"postal_code"}, - }, - }, - FormattedAddress: "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA", - Geometry: AddressGeometry{ - Location: LatLng{Lat: 37.4224764, Lng: -122.0842499}, - Bounds: LatLngBounds{ - NorthEast: LatLng{Lat: 37.4238253802915, Lng: -122.0829009197085}, - SouthWest: LatLng{Lat: 37.4211274197085, Lng: -122.0855988802915}, - }, - LocationType: "ROOFTOP", - Viewport: LatLngBounds{ - NorthEast: LatLng{Lat: 37.4238253802915, Lng: -122.0829009197085}, - SouthWest: LatLng{Lat: 37.4211274197085, Lng: -122.0855988802915}, - }, - Types: nil, + Results: []GeocodingResult{ + { + AddressComponents: []AddressComponent{ + { + LongName: "1600", + ShortName: "1600", + Types: []string{"street_number"}, + }, + { + LongName: "Amphitheatre Pkwy", + ShortName: "Amphitheatre Pkwy", + Types: []string{"route"}, + }, + { + LongName: "Mountain View", + ShortName: "Mountain View", + Types: []string{"locality", "political"}, + }, + { + LongName: "Santa Clara County", + ShortName: "Santa Clara County", + Types: []string{"administrative_area_level_2", "political"}, + }, + { + LongName: "California", + ShortName: "CA", + Types: []string{"administrative_area_level_1", "political"}, + }, + { + LongName: "United States", + ShortName: "US", + Types: []string{"country", "political"}, + }, + { + LongName: "94043", + ShortName: "94043", + Types: []string{"postal_code"}, + }, + }, + FormattedAddress: "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA", + Geometry: AddressGeometry{ + Location: LatLng{Lat: 37.4224764, Lng: -122.0842499}, + Bounds: LatLngBounds{ + NorthEast: LatLng{Lat: 37.4238253802915, Lng: -122.0829009197085}, + SouthWest: LatLng{Lat: 37.4211274197085, Lng: -122.0855988802915}, + }, + LocationType: "ROOFTOP", + Viewport: LatLngBounds{ + NorthEast: LatLng{Lat: 37.4238253802915, Lng: -122.0829009197085}, + SouthWest: LatLng{Lat: 37.4211274197085, Lng: -122.0855988802915}, + }, + Types: nil, + }, + PartialMatch: false, + PlaceID: "ChIJ2eUgeAK6j4ARbn5u_wAGqWA", + Types: []string{"street_address"}, }, - PartialMatch: false, - PlaceID: "ChIJ2eUgeAK6j4ARbn5u_wAGqWA", - Types: []string{"street_address"}, }, } @@ -312,73 +314,75 @@ func TestGeocodingReverseGeocoding(t *testing.T) { resp, err := c.ReverseGeocode(context.Background(), r) - if len(resp) != 1 { - t.Errorf("expected %+v, was %+v", 1, len(resp)) + if len(resp.Results) != 1 { + t.Errorf("expected %+v, was %+v", 1, len(resp.Results)) } if err != nil { t.Errorf("r.Get returned non nil error: %v", err) } correctResponse := GeocodingResponse{ - Results: GeocodingResult{ - AddressComponents: []AddressComponent{ - { - LongName: "277", - ShortName: "277", - Types: []string{"street_number"}, - }, - { - LongName: "Bedford Avenue", - ShortName: "Bedford Ave", - Types: []string{"route"}, - }, - { - LongName: "Williamsburg", - ShortName: "Williamsburg", - Types: []string{"neighborhood", "political"}, - }, - { - LongName: "Brooklyn", - ShortName: "Brooklyn", - Types: []string{"sublocality", "political"}, - }, - { - LongName: "Kings", - ShortName: "Kings", - Types: []string{"administrative_area_level_2", "political"}, - }, - { - LongName: "New York", - ShortName: "NY", - Types: []string{"administrative_area_level_1", "political"}, - }, - { - LongName: "United States", - ShortName: "US", - Types: []string{"country", "political"}, - }, - { - LongName: "11211", - ShortName: "11211", - Types: []string{"postal_code"}, - }, - }, - FormattedAddress: "277 Bedford Avenue, Brooklyn, NY 11211, USA", - Geometry: AddressGeometry{ - Location: LatLng{Lat: 40.714232, Lng: -73.9612889}, - Bounds: LatLngBounds{ - NorthEast: LatLng{Lat: 40.7155809802915, Lng: -73.9599399197085}, - SouthWest: LatLng{Lat: 40.7128830197085, Lng: -73.96263788029151}, - }, - LocationType: "ROOFTOP", - Viewport: LatLngBounds{ - NorthEast: LatLng{Lat: 40.7155809802915, Lng: -73.9599399197085}, - SouthWest: LatLng{Lat: 40.7128830197085, Lng: -73.96263788029151}, - }, - Types: nil, + Results: []GeocodingResult{ + { + AddressComponents: []AddressComponent{ + { + LongName: "277", + ShortName: "277", + Types: []string{"street_number"}, + }, + { + LongName: "Bedford Avenue", + ShortName: "Bedford Ave", + Types: []string{"route"}, + }, + { + LongName: "Williamsburg", + ShortName: "Williamsburg", + Types: []string{"neighborhood", "political"}, + }, + { + LongName: "Brooklyn", + ShortName: "Brooklyn", + Types: []string{"sublocality", "political"}, + }, + { + LongName: "Kings", + ShortName: "Kings", + Types: []string{"administrative_area_level_2", "political"}, + }, + { + LongName: "New York", + ShortName: "NY", + Types: []string{"administrative_area_level_1", "political"}, + }, + { + LongName: "United States", + ShortName: "US", + Types: []string{"country", "political"}, + }, + { + LongName: "11211", + ShortName: "11211", + Types: []string{"postal_code"}, + }, + }, + FormattedAddress: "277 Bedford Avenue, Brooklyn, NY 11211, USA", + Geometry: AddressGeometry{ + Location: LatLng{Lat: 40.714232, Lng: -73.9612889}, + Bounds: LatLngBounds{ + NorthEast: LatLng{Lat: 40.7155809802915, Lng: -73.9599399197085}, + SouthWest: LatLng{Lat: 40.7128830197085, Lng: -73.96263788029151}, + }, + LocationType: "ROOFTOP", + Viewport: LatLngBounds{ + NorthEast: LatLng{Lat: 40.7155809802915, Lng: -73.9599399197085}, + SouthWest: LatLng{Lat: 40.7128830197085, Lng: -73.96263788029151}, + }, + Types: nil, + }, + PlaceID: "ChIJd8BlQ2BZwokRAFUEcm_qrcA", + Types: []string{"street_address"}, }, - PlaceID: "ChIJd8BlQ2BZwokRAFUEcm_qrcA", - Types: []string{"street_address"}, }, } @@ -636,64 +640,66 @@ func TestReverseGeocodingPlaceID(t *testing.T) { } resp, err := c.ReverseGeocode(context.Background(), r) - if len(resp) != 1 { - t.Errorf("Expected length of response is 1, was %+v", len(resp)) + if len(resp.Results) != 1 { + t.Errorf("Expected length of response is 1, was %+v", len(resp.Results)) } if err != nil { t.Errorf("r.Get returned non nil error: %v", err) } correctResponse := GeocodingResponse{ - Results: GeocodingResult{ - AddressComponents: []AddressComponent{ - { - LongName: "1600", - ShortName: "1600", - Types: []string{"street_number"}, - }, - { - LongName: "Amphitheatre Pkwy", - ShortName: "Amphitheatre Pkwy", - Types: []string{"route"}, - }, - { - LongName: "Mountain View", - ShortName: "Mountain View", - Types: []string{"locality", "political"}, - }, - { - LongName: "Santa Clara County", - ShortName: "Santa Clara County", - Types: []string{"administrative_area_level_2", "political"}, - }, - { - LongName: "California", - ShortName: "CA", - Types: []string{"administrative_area_level_1", "political"}, - }, - { - LongName: "United States", - ShortName: "US", - Types: []string{"country", "political"}, - }, - { - LongName: "94043", - ShortName: "94043", - Types: []string{"postal_code"}, - }, - }, - FormattedAddress: "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA", - Geometry: AddressGeometry{ - Location: LatLng{Lat: 37.4224764, Lng: -122.0842499}, - LocationType: "ROOFTOP", - Viewport: LatLngBounds{ - NorthEast: LatLng{Lat: 37.4238253802915, Lng: -122.0829009197085}, - SouthWest: LatLng{Lat: 37.4211274197085, Lng: -122.0855988802915}, - }, - Types: nil, + Results: []GeocodingResult{ + { + AddressComponents: []AddressComponent{ + { + LongName: "1600", + ShortName: "1600", + Types: []string{"street_number"}, + }, + { + LongName: "Amphitheatre Pkwy", + ShortName: "Amphitheatre Pkwy", + Types: []string{"route"}, + }, + { + LongName: "Mountain View", + ShortName: "Mountain View", + Types: []string{"locality", "political"}, + }, + { + LongName: "Santa Clara County", + ShortName: "Santa Clara County", + Types: []string{"administrative_area_level_2", "political"}, + }, + { + LongName: "California", + ShortName: "CA", + Types: []string{"administrative_area_level_1", "political"}, + }, + { + LongName: "United States", + ShortName: "US", + Types: []string{"country", "political"}, + }, + { + LongName: "94043", + ShortName: "94043", + Types: []string{"postal_code"}, + }, + }, + FormattedAddress: "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA", + Geometry: AddressGeometry{ + Location: LatLng{Lat: 37.4224764, Lng: -122.0842499}, + LocationType: "ROOFTOP", + Viewport: LatLngBounds{ + NorthEast: LatLng{Lat: 37.4238253802915, Lng: -122.0829009197085}, + SouthWest: LatLng{Lat: 37.4211274197085, Lng: -122.0855988802915}, + }, + Types: nil, + }, + PlaceID: "ChIJ2eUgeAK6j4ARbn5u_wAGqWA", + Types: []string{"street_address"}, }, - PlaceID: "ChIJ2eUgeAK6j4ARbn5u_wAGqWA", - Types: []string{"street_address"}, }, AddressDescriptor: AddressDescriptor{ Landmarks: []Landmark{ @@ -701,35 +707,35 @@ func TestReverseGeocodingPlaceID(t *testing.T) { PlaceID: "ChIJvUbrwCCoAWARX2QiHCsn5A4", DisplayName: LocalizedText{Text: "Kinkaku-ji", LanguageCode: "en"}, Types: []string{"establishment", "place_of_worship", "point_of_interest", "tourist_attraction"}, - SpatialRelationship: NEAR, + SpatialRelationship: SPATIAL_RELATIONSHIP_NEAR, StraightLineDistanceMeters: 0.009104185, }, { PlaceID: "ChIJf2s61SCoAWARVtK8cnSu6zw", DisplayName: LocalizedText{Text: "Shariden Kinkaku", LanguageCode: "en"}, Types: []string{"establishment", "place_of_worship", "point_of_interest", "tourist_attraction"}, - SpatialRelationship: WITHIN, + SpatialRelationship: SPATIAL_RELATIONSHIP_WITHIN, StraightLineDistanceMeters: 73.58092, }, { PlaceID: "ChIJXZeF2jipAWARNbF8pJDRjFc", DisplayName: LocalizedText{Text: "Kyōko-chi Pond", LanguageCode: "en"}, Types: []string{"establishment", "park", "point_of_interest"}, - SpatialRelationship: BEHIND, + SpatialRelationship: SPATIAL_RELATIONSHIP_BEHIND, StraightLineDistanceMeters: 57.99922, }, { PlaceID: "ChIJj69vLCapAWAR0FBBPEfPeAQ", DisplayName: LocalizedText{Text: "鹿苑寺(金閣寺)", LanguageCode: "ja"}, Types: []string{"establishment", "place_of_worship", "point_of_interest"}, - SpatialRelationship: WITHIN, + SpatialRelationship: SPATIAL_RELATIONSHIP_WITHIN, StraightLineDistanceMeters: 32.30453, }, { PlaceID: "ChIJ482HblCpAWARoLBXDZpv7aI", DisplayName: LocalizedText{Text: "Kinkaku-ji Fence", LanguageCode: "en"}, Types: []string{"establishment", "point_of_interest"}, - SpatialRelationship: WITHIN, + SpatialRelationship: SPATIAL_RELATIONSHIP_WITHIN, StraightLineDistanceMeters: 99.38629, }, }, @@ -737,12 +743,12 @@ func TestReverseGeocodingPlaceID(t *testing.T) { { PlaceID: "ChIJe9XMwiCoAWARVrQpOsYqdBE", DisplayName: LocalizedText{Text: "Kinkakujicho", LanguageCode: "en"}, - Containment: WITHIN, + Containment: CONTAINMENT_WITHIN, }, { PlaceID: "ChIJk-6T5COoAWARa-KMWGWzrwQ", DisplayName: LocalizedText{Text: "Kinkaku-ji", LanguageCode: "en"}, - Containment: OUTSKIRTS, + Containment: CONTAINMENT_OUTSKIRTS, }, }, }, @@ -791,11 +797,11 @@ func TestGeocodingZeroResults(t *testing.T) { t.Errorf("Unexpected error for ZERO_RESULTS status") } - if response == nil { + if response.Results == nil { t.Errorf("Unexpected nil response for ZERO_RESULTS status") } - if len(response) != 0 { + if len(response.Results) != 0 { t.Errorf("Unexpected response for ZERO_RESULTS status") } } @@ -814,11 +820,11 @@ func TestReverseGeocodingZeroResults(t *testing.T) { t.Errorf("Unexpected error for ZERO_RESULTS status") } - if response == nil { + if response.Results == nil { t.Errorf("Unexpected nil response for ZERO_RESULTS status") } - if len(response) != 0 { + if len(response.Results) != 0 { t.Errorf("Unexpected response for ZERO_RESULTS status") } }