diff --git a/maps.go b/maps.go index 1be838f..ee87d93 100644 --- a/maps.go +++ b/maps.go @@ -85,3 +85,7 @@ func encodeLocations(ls []Location) string { } return strings.Join(s, "|") } + +func Float64(f float64) *float64 { + return &f +} diff --git a/maps_test.go b/maps_test.go index 456d07b..6b182d7 100644 --- a/maps_test.go +++ b/maps_test.go @@ -75,3 +75,17 @@ func TestStaticMap(t *testing.T) { t.Errorf("unexpected error: %v", err) } } + +func TestStreetView(t *testing.T) { + c := NewClient("") + s := Size{600, 300} + opts := &StreetViewOpts{ + Location: &LatLng{46.414382, 10.013988}, + Heading: Float64(151.78), + Pitch: -0.76, + } + t.Logf("%s", baseURL+streetview(s, opts)) + if _, err := c.StreetView(s, opts); err != nil { + t.Errorf("unexpected error: %v", err) + } +} diff --git a/streetview.go b/streetview.go new file mode 100644 index 0000000..90bb1b8 --- /dev/null +++ b/streetview.go @@ -0,0 +1,55 @@ +package maps + +import ( + "fmt" + "io" + "net/http" + "net/url" +) + +func (c Client) StreetView(s Size, opts *StreetViewOpts) (io.ReadCloser, error) { + resp, err := c.do(baseURL + streetview(s, opts)) + if err != nil { + return nil, err + } + if resp.StatusCode != http.StatusOK { + return nil, HTTPError{resp} + } + return resp.Body, nil +} + +func streetview(s Size, opts *StreetViewOpts) string { + p := url.Values{} + p.Set("size", s.String()) + opts.update(p) + return "streetview?" + p.Encode() +} + +type StreetViewOpts struct { + Location Location + Pano string // panoramio ID + Heading *float64 // 0-360 + FOV *float64 // 0-120, 90 default + Pitch float64 // -90 to +90 +} + +func (s *StreetViewOpts) update(p url.Values) { + if s == nil { + return + } + if s.Location != nil { + p.Set("location", s.Location.Location()) + } + if s.Pano != "" { + p.Set("pano", s.Pano) + } + if s.Heading != nil { + p.Set("heading", fmt.Sprintf("%f", *s.Heading)) + } + if s.FOV != nil { + p.Set("fov", fmt.Sprintf("%f", *s.FOV)) + } + if s.Pitch != 0 { + p.Set("pitch", fmt.Sprintf("%f", s.Pitch)) + } +}