Step 3–Augmented Reality, Windows 8, and Cloud Computing–How to implement with real code (Implementing the Cloud Back-End)
[アーティクル]
Title
Description
Link
Augmented Reality applications need a Web Service Back-End. Here is a 90-Day No obligation, totally free offer to use Windows Azure as your web service for Windows 8 Clients.
You get: Compute / 750 small compute hours per month, Web sites / 10 web sites, Mobile services / 10 mobile services, Relational database / 1 SQL database, SQL reporting / 100 hours per month, Storage / 35GB with 50,000,000 storage transactions, Bandwidth / unlimited inbound & 25GB outbound, CDN / 20GB outbound with 500,000 transactions, Cache / 128MB, Service bus / 1,500 relay hours and 500,000 messages
Step 4–Augmented Reality, Windows 8, and Cloud Computing–How to implement with real code ...
This post provides all source code and explanation for the Windows 8 Client (Augmented Reality Demo). The augmented reality Windows 8 Client calls into the Azure back-end described in step 3 above.
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Net.Http;using System.Net.Http.Formatting;using System.Text;using System.Threading.Tasks;using System.Xml;using Location_WebRole.HelperObjects;namespace Location_WebRole.Controllers{// My custom string manipulator. Nows how to parse all the XML structurespublicclass CityFromGps{// Method that does all the lookups based on longitude and latitude.public LocationInfo ReverseGeoLoc(string longitude, string latitude){// Return data is LocationInfoLocationInfo loc_info = new LocationInfo();DataExtractor de = new DataExtractor(); // a helper class to make this threading code readableloc_info.longitude = longitude;loc_info.latitude = latitude;try{//// Get Data//XmlDocument[] xmldocs = new XmlDocument[3] { new XmlDocument(), new XmlDocument(), new XmlDocument() };Task<string>[] tasks = new Task<string>[3]{Task<string>.Factory.StartNew(() =>{xmldocs[0].Load("http://maps.googleapis.com/maps/api/elevation/xml?locations=" + latitude + "," + longitude + "&sensor=false");XmlNodeList weatherList = xmldocs[0].SelectNodes("//ElevationResponse/result");loc_info.elevation = (Convert.ToDouble(weatherList[0]["elevation"].InnerText) * 3.2808399).ToString();return"";}),Task<string>.Factory.StartNew(() =>{// Get Transit and Neighborhood Infoxmldocs[1].Load(string.Format("http://maps.googleapis.com/maps/api/geocode/xml?latlng={0},{1}&sensor=false", latitude, longitude));loc_info.bus_and_neighborhood = de.GetTransitAndNeighborhood(xmldocs[1]);return"";}),Task<string>.Factory.StartNew(() =>{// Get Weather Infoxmldocs[2].Load(string.Format("http://graphical.weather.gov/xml/sample_products/browser_interface/ndfdXMLclient.php?lat={0}&lon={1}&product=time-series&maxt=maxt&mint=mint", latitude, longitude));loc_info.max_temp = de.GetMaxTemp(xmldocs[2]);loc_info.min_temp = de.GetMinTemp(xmldocs[2]);return"";})};//Block until all tasks complete. This is much faster.Task.WaitAll(tasks);return loc_info;}catch (Exception ex){loc_info.error = "(Location lookup failed: ) " + ex.Message;return loc_info;}}}}
Code Walkthrough
Lines 1 to 10
Needed using statements
Line 15
CityFromGps class that we've added
Line 19
The ReverseGeoLoc() method. It returns JSON-formatted data for LocationInfo (a class we still need to add). It takes two parameters, logitude and latitude. It will perform a number of look ups against other web services, such as Google Maps and NOAA (NOAA - National Oceanic and Atmospheric Administration)
Lines 22 and 23
Instantiate our helper objects. LocationInfo will contain the results of our data lookups. It will hold temperature, elevation, city, etc.
Line 33
The XmlDocument objects that will be used for the lookups against web services
Lines 37, 44, 51
3 Tasks (concurrent threads if enough cores) are launched, working in parallel, performing lookups against web services.
Lines 40-41, 47-48, 54-56
The actual lookups. This where the actual call to Google Maps and NOAA takes place.
Line 61
Wait for all tasks to complete. This line ensures that we continue only after all 3 tasks have completed.
Line 62
Return the LocationInfo object, which is now populated with data. The ASP.NET Web API automatically makes this a JSON formatted object.
DataExtractor.cs and LocationInfo.cs
DataExtractor.cs abstracts the complexity of parsing XML and getting the answers we need
Such as max temp, min temp, elevation, etc
LocationInfo.cs represents the data going back to the Windows 8 client
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Xml;namespace Location_WebRole.Controllers{publicclass DataExtractor{publicstring GetTransitAndNeighborhood(XmlDocument doc){string result = "";bool foundneighborhood = false;bool foundbus = false;XmlNodeList xnList = doc.SelectNodes("//GeocodeResponse/result/address_component");foreach (XmlNode xn in xnList){try{switch (xn["type"].InnerText){//Add whatever you are looking for belowcase"bus_station":{if (foundbus)break;if (result != "")result += ", ";result += "Bus: " + xn["long_name"].InnerText;foundbus = true;break;}case"neighborhood":{if (foundneighborhood)break;if (result != "")result += ", ";result += "Neighborhood: " + xn["long_name"].InnerText + " (" + xn["short_name"].InnerText + ")";foundneighborhood = true;break;}default:break;}}catch (Exception e){}}return result;}internalstring GetMaxTemp(XmlDocument doc){XmlNodeList xnList = doc.SelectNodes("//dwml/data/parameters/temperature");return xnList[0]["value"].InnerText; // offset 0 is max temps}internalstring GetMinTemp(XmlDocument doc){XmlNodeList xnList = doc.SelectNodes("//dwml/data/parameters/temperature");return xnList[1]["value"].InnerText; // offset 1 is min temps}}}
Code Walkthrough
Line 11
The method that returns the bus stop and neighborhood information.
Line 60
The method that returns the max temperature info from NOAA
Line 65
The method that returns the min temperature info from NOAA
LocationInfo.cs
12345678910111213141516171819202122
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Net.Http;using System.Net.Http.Formatting;using System.Web;namespace Location_WebRole.HelperObjects{publicclass LocationInfo{// Data we will lookuppublicstring latitude { get; set; }publicstring longitude { get; set; }publicstring elevation { get; set; }publicstring bus_and_neighborhood { get; set; }publicstring max_temp { get; set; }publicstring min_temp { get; set; }publicstring error { get; set; }}}
Code Walkthrough
Line 11
Our LocationInfo structure. This will be returned back to the Windows 8 client, populated with data. The ASP.NET Web API will automatically convert this object to JSON format.
Conclusion
The next series of posts will focus on the Windows 8 client.
Comments
Anonymous
February 11, 2013
The comment has been removed