Location Aware Silverlight 3 Applications
Posted by: Clarity Blogs: ASP.NET,
on 21 Aug 2009 |
View original | Bookmarked: 0 time(s)
Adding location awareness makes ever application better. Like adding chili to any food item makes it awesomer. Google has a nice AJAX API for finding your current location (for details see http://code.google.com/apis/ajax/documentation/)
To see an example of getting the location here: http://employees.claritycon.com/kmarshall/blog/files/slgeocode/
Since its based of your IP address it doesnt always work well depending on your ISP or if you are VPNd into another network. Also SL3 out of browser apps have no DOM to access so you cant run JS code. In that case I have a separate function to ping http://api.hostip.info/get_html.php?position=true The Google results seem a bit better so the application checks if its OOB . If it isnt then it uses the Google APIs and if it is OOB or the Google API call returns no result then it tries hostip.info. In Silverlight you can easily interop with javascript through the html bridge.
As for the code, in the page that hosts your application add:
<script src="http://www.google.com/jsapi?key=YOUR_API_KEY
"
type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
google.load("maps", "2");
function GetCurrentLocation() {
if (google.loader.ClientLocation) {
return google.loader.ClientLocation;
}
}
//]]>
</script>
In Silverlight you can invoke that function:
public Location GetCurrentLocationFromJS()
{
//dom is available so call the google location api in js
ScriptObject scriptObject = HtmlPage.Window.Invoke("GetCurrentLocation") as ScriptObject;
if (scriptObject != null)
return scriptObject.ConvertTo<Location>();
else
return null;
}
The convertTo method will de-serialize the javascript object if there is a class with a matching definition. Otherwise you could create JSON in the host page and pass that back.
public class Location
{
public string latitude { get; set; }
public string longitude { get; set; }
public Address address { get; set; }
public class Address
{
public string city { get; set; }
public string country { get; set; }
public string country_code { get; set; }
public string region { get; set; }
}
}
The code to get the location from the hostip.info is just a normal webclient request. In the demo application, it also uses the Yahoo geocoding service so you can enter a location or place name like Golden Gate Park and it will calculate the distance. I tried using Yahoo Placemaker, but that does not allow cross domain calls. Although someone messaged me on Twitter that you can use Placemaker via YQL which does allow cross domain calls. This code was part a experiment to see if we could geotag tweets/bios in a SL twitter client and filter on results relevant to your current location. Even though twitter is going to add location info to the API, I still think its potentially interesting to infer locations through Placemaker e.g Someone sends a message Going to Union Square and you can get the coordinate of Union Square and others filtering on 10 miles of San Francisco will see the message".
Demo source files