Skip to content

Commit 312225a

Browse files
authored
Merge pull request #136 from bdjankowski/StreetViewAdditions
Enhance street view requests
2 parents 27e5ac2 + 9053ca1 commit 312225a

File tree

9 files changed

+184
-23
lines changed

9 files changed

+184
-23
lines changed

src/Google.Maps.Test/StreetView/StreetViewRequestTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ public void Pitch_argumentoutofrange(short badvalue)
4646
});
4747
}
4848

49+
[Test]
50+
[TestCase(121)]
51+
[TestCase(-1)]
52+
[TestCase(0)]
53+
public void FieldOfView_argumentoutofrange(short badvalue)
54+
{
55+
Assert.Throws<ArgumentOutOfRangeException>(() =>
56+
{
57+
StreetViewRequest sm = new StreetViewRequest()
58+
{
59+
FieldOfView = badvalue
60+
};
61+
});
62+
}
63+
4964
[Test]
5065
[TestCase(-1)]
5166
[TestCase(361)]

src/Google.Maps/Constants.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ internal static void CheckPitchRange(short value, string parameterName)
8888
if(value < -90 || value > 90) throw new ArgumentOutOfRangeException(PITCH_PARAMETER_RANGE, parameterName);
8989
}
9090

91+
internal static void CheckFieldOfViewRange(short value, string parameterName)
92+
{
93+
const string FIELD_OF_VIEW_PARAMETER_RANGE = "Field of view value must be greater or equal to 1 and less than or equal to 120.";
94+
if (value < 1 || value > 120) throw new ArgumentOutOfRangeException(FIELD_OF_VIEW_PARAMETER_RANGE, parameterName);
95+
}
96+
9197
#endregion
9298

9399
}

src/Google.Maps/GoogleSigned.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
52
using System.Security.Cryptography;
3+
using System.Text;
64

75
namespace Google.Maps
86
{
@@ -16,6 +14,8 @@ public class GoogleSigned
1614
{
1715
private byte[] _privateKeyBytes;
1816
private string _idString;
17+
private string _channelId;
18+
private string _referralUrl;
1919
private GoogleSignedType _signType = GoogleSignedType.ApiKey;
2020

2121
/// <summary>
@@ -106,6 +106,9 @@ public static GoogleSigned SigningInstance
106106
}
107107

108108

109+
public string ReferralUrl { get { return _referralUrl; } }
110+
111+
109112
/// <summary>
110113
/// Assigns the given SigningInstance to all services that can utilize it. Note that some of the services do not currently use the signature method.
111114
/// </summary>
@@ -130,6 +133,13 @@ public string GetSignedUri(Uri uri)
130133
if(_signType == GoogleSignedType.Business)
131134
{
132135
builder.Query = builder.Query.Substring(1) + "&client=" + _idString;
136+
137+
138+
if (!string.IsNullOrEmpty(_channelId))
139+
{
140+
builder.Query = builder.Query.Substring(1) + "&channel=" + _channelId;
141+
}
142+
133143
}
134144
else
135145
{

src/Google.Maps/Internal/MapsHttp.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@
1515
* limitations under the License.
1616
*/
1717

18+
using Newtonsoft.Json;
1819
using System;
1920
using System.Collections.Generic;
21+
using System.IO;
2022
using System.Net.Http;
2123
using System.Threading.Tasks;
22-
using System.IO;
23-
24-
using Newtonsoft.Json;
2524

2625
namespace Google.Maps.Internal
2726
{
@@ -39,6 +38,11 @@ public MapsHttp(GoogleSigned signingSvc)
3938
{
4039
this.signingSvc = signingSvc;
4140
this.client = new HttpClient();
41+
42+
if (signingSvc != null && string.IsNullOrEmpty(signingSvc.ReferralUrl) == false)
43+
{
44+
client.DefaultRequestHeaders.Add("Referer", signingSvc.ReferralUrl);
45+
}
4246
}
4347

4448
public async Task<T> GetAsync<T>(Uri uri) where T : class
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace Google.Maps.StreetView
2+
{
3+
public abstract class StreetViewBase : BaseRequest
4+
{
5+
/// <summary>
6+
/// Defines the center of the map, equidistant from all edges of the
7+
/// map. This parameter takes an <see cref="Location" />-derived instance identifying a
8+
/// unique location on the face of the earth. Use <see cref="LatLng" /> for a
9+
/// {latitude,longitude} pair (e.g. 40.714728,-73.998672) or use <see cref="Location" /> for a
10+
/// string address (e.g. "city hall, new york, ny"). Either Location or PanoramaId is required.
11+
/// </summary>
12+
public Location Location { get; set; }
13+
14+
/// <summary>
15+
/// PanoramaId is a specific panorama ID. These are generally stable.
16+
/// Either Location or PanoramaId is required.
17+
/// </summary>
18+
public string PanoramaId { get; set; }
19+
}
20+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+

2+
using System;
3+
4+
using Google.Maps.Internal;
5+
6+
namespace Google.Maps.StreetView
7+
{
8+
/// <summary>
9+
/// The Google Static Maps API returns metadata about availablility
10+
/// in response to a HTTP request via a URL. For each request, you can
11+
/// specify the <see cref="StreetViewBase.Location"/> of the map, or <see cref="StreetViewBase.PanoramaId"/>,
12+
/// </summary>
13+
public class StreetViewMetadataRequest : StreetViewBase
14+
{
15+
public override Uri ToUri()
16+
{
17+
QueryStringBuilder qs = new QueryStringBuilder();
18+
19+
if (this.Location != null)
20+
{
21+
qs.Append("location", this.Location.GetAsUrlParameter());
22+
}
23+
else if (string.IsNullOrEmpty(this.PanoramaId) == false)
24+
{
25+
qs.Append("pano", this.PanoramaId);
26+
}
27+
else
28+
{
29+
throw new InvalidOperationException("Either Location or PanoramaId property are required.");
30+
}
31+
32+
var url = "streetview/metadata?" + qs.ToString();
33+
34+
return new Uri(StreetViewService.HttpsUri, new Uri(url, UriKind.Relative));
35+
}
36+
}
37+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
using System;
19+
20+
using Google.Maps.Common;
21+
22+
using Newtonsoft.Json;
23+
24+
25+
namespace Google.Maps.StreetView
26+
{
27+
[JsonObject(MemberSerialization.OptIn)]
28+
public class StreetViewMetadataResponse : IServiceResponse
29+
{
30+
/// <summary>
31+
/// The response location
32+
/// </summary>
33+
[JsonProperty("location")]
34+
public LatLng Location { get; set; }
35+
36+
/// <summary>
37+
/// The panorama id
38+
/// </summary>
39+
[JsonProperty("pano_id")]
40+
public string PanoId { get; set; }
41+
42+
/// <inheritdoc />
43+
[JsonProperty("status")]
44+
public ServiceResponseStatus Status { get; set; }
45+
46+
47+
/// <inheritdoc />
48+
[JsonProperty("error_message")]
49+
public string ErrorMessage { get; set; }
50+
51+
/// <summary>
52+
/// The copyright info for the image if requested
53+
/// </summary>
54+
[JsonProperty("copyright")]
55+
public string Copywrite { get; set; }
56+
57+
/// <summary>
58+
/// Date photo was taken
59+
/// </summary>
60+
[JsonProperty("date")]
61+
public DateTime Date { get; set; }
62+
63+
}
64+
}

src/Google.Maps/StreetView/StreetViewRequest.cs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,13 @@ namespace Google.Maps.StreetView
3131
/// the type of map, and the placement of optional markers at locations on
3232
/// the map.
3333
/// </summary>
34-
public class StreetViewRequest : BaseRequest
34+
public class StreetViewRequest : StreetViewBase
3535
{
3636
public StreetViewRequest()
3737
{
3838
this.Size = new MapSize(512, 512); //default size is 512x512
3939
}
4040

41-
/// <summary>
42-
/// Defines the center of the map, equidistant from all edges of the
43-
/// map. This parameter takes an <see cref="Location" />-derived instance identifying a
44-
/// unique location on the face of the earth. Use <see cref="LatLng" /> for a
45-
/// {latitude,longitude} pair (e.g. 40.714728,-73.998672) or use <see cref="Location" /> for a
46-
/// string address (e.g. "city hall, new york, ny"). Either Location or PanoramaId is required.
47-
/// </summary>
48-
public Location Location { get; set; }
49-
50-
/// <summary>
51-
/// PanoramaId is a specific panorama ID. These are generally stable.
52-
/// Either Location or PanoramaId is required.
53-
/// </summary>
54-
public string PanoramaId { get; set; }
55-
56-
5741
/// <summary>
5842
/// Size specifies the output size of the image in pixels.
5943
/// For example Size = new MapSize(600,400) returns an image 600 pixels wide, and 400 high.
@@ -124,6 +108,20 @@ public short Pitch
124108
}
125109
private short _pitch;
126110

111+
/// <summary>
112+
/// Field of view
113+
/// </summary>
114+
[DefaultValue(90)]
115+
public short FieldOfView
116+
{
117+
get { return _fieldOfView; }
118+
set
119+
{
120+
Constants.CheckFieldOfViewRange(value, "value");
121+
_fieldOfView = value;
122+
}
123+
}
124+
private short _fieldOfView;
127125

128126
/// <summary>
129127
/// Builds the request uri parameters

src/Google.Maps/StreetView/StreetViewService.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ public async Task<byte[]> GetImageAsync(StreetViewRequest request)
5656
return StreamToArray(stream);
5757
}
5858

59+
public StreetViewMetadataResponse GetMetadataResponse(StreetViewMetadataRequest request)
60+
{
61+
var uri = new Uri(baseUri, request.ToUri());
62+
63+
return http.Get<StreetViewMetadataResponse>(uri);
64+
}
65+
5966
public Stream GetStream(StreetViewRequest request)
6067
{
6168
var uri = new Uri(baseUri, request.ToUri());

0 commit comments

Comments
 (0)