Thursday, November 21, 2013

C# code snippet to Consume Google StreetViewImage API




The Google Street View Image API lets you embed a static(non-interactive) Street View panorama or thumbnail into your web page, without the use of JavaScript. The viewport is defined with URL parameters sent through a standard HTTP request, and is returned as a static image.

Following is c# sample to connect with Google StreetViewImage API (Non-interactive, static street view image) which doesn't involve any javascript. Code utilizes simple WebClient class over HttpWebRequest class and also provides asynchronous operations.

Both classes are from System.Net namespace. WebClient is a nice helper class that provides simpler way to download resource from web like files, images in synchronous or asynchronous fashion. If you need more fine grained control over web requests and responses, better use HttpWebRequest/HttpWebResponse.

WebClient.DownloadDataTaskAsync - Downloads the resource as a Byte array from the URI specified as an asynchronous operation. These methods do not block the calling thread.

BTW, You might ask why did I use Task.wait().. In my case, my task is synchronous in nature. it will block synchronously until the task is complete. Instead, if you want to make it truly asynchronous, make the method async and use await keyword to wait asynchronously while keeping your calling thread unblocked.

All the required/optional parameters are documented below in comments.

Following is the typical HTTP request we need to consume street view image

http://maps.googleapis.com/maps/api/streetview?size=400x400&location=40.720032,%20-73.988354&fov=90&heading=235&pitch=10&sensor=false

/// location can be either text string (such as "Nuevo San Juan Parangaricutiro" or lat/lng values(40.457375,-80.009353)

///  specifies the output size of the image in pixels. Size is specified as {width} x {height} 

/// - for ex: size=600x400 returns an image 600 pixels wide, and 400 high.

/// Street View images can be returned in any size up to 640 by 640 pixels.

/// /*******OPTIONAL PARAMETERS: ************************/

/// ************************************************************************************************************

/// 1) HEADING

/// Indicates the compass heading of the camera. 

/// Accepted values are from 0 to 360 (both values indicating North, with 90 indicating East, and 180 South). 

/// If no heading is specified, a value will be calculated that directs the camera towards the specified location, 

/// from the point at which the closest photograph was taken.

/// *************************************************************************************************************

/// 2)FOV - field of view

/// fov (default is 90) determines the horizontal field of view of the image. The field of view is expressed in degrees, 

/// with a maximum allowed value of 120. When dealing with a fixed-size viewport, as with a Street View image of a set size, 

/// field of view in essence represents zoom, with smaller numbers indicating a higher level of zoom.

/// *************************************************************************************************************

/// 3)PITCH - (default is 0) specifies the up or down angle of the camera relative to the Street View vehicle. This is often, 

/// but not always, flat horizontal. Positive values angle the camera up (with 90 degrees indicating straight up); 

/// negative values angle the camera down (with -90 indicating straight down).

/// *****************************************************************************************************************

/// 4)KEY (optional) identifies your application for quota purposes, and enables reports in the APIs Console. 

/// ***********************************************************************************************************************

///5)SENSOR - indicates whether or not the request came from a device using a location sensor (e.g. a GPS) to determine the location sent in this request. This value must be either true or false.

/// 

public byte[] GetStreetViewImage(string location, string size =null)

{

try

{

var streetViewImageServiceUrl = "http://maps.googleapis.com/maps/api/streetview?size=400x400&location=40.720032,%20-73.988354&fov=90&heading=235&pitch=10&sensor=false"

if (string.IsNullOrEmpty(location))

 {

throw new ArgumentNullException(@"location argument is missing.");

}



var imageUrl  = !string.IsNullOrEmpty(size) ?

string.Format("{0}&size={1}", string.Format("{0}&location={1}", streetViewImageServiceUrl, location), size) :

string.Format("{0}&size={1}", string.Format("{0}&location={1}", streetViewImageServiceUrl, location), StreetViewImageSize);
using (var cli = new WebClient())

{

var task = cli.DownloadDataTaskAsync(imageUrl);

task.Wait();

return task.Result;

}



 }

catch {

}

}