A little enlightenment
Yesterday I was working on a particularly perplexing problem where I had a need to have the Silverlight project (IntegraConnect in this case) know the exact location of the machine that was accessing it. The idea being that on a map centered on the users location could then show points of interest around their location. In this case the client would have their service coordinators in the field perhaps at a person home directing those persons to community resources, which is a prime directive for this particular client using the IntegraConnect system.
The problem is that Silverlight does not have an API to communicate with GPS hardware or even an API that will try to determine the location based on the IP address, but modern browsers do. So the problem was how to get that information from the browser. Turns out that there are copious examples of Javascript code that gets the coordinates out of the browser and place them in an alert or use them to message a Google map or some other such contrived thing… That code basically follows this simple pattern
function getCurrentLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showMyPosition, showError);
}
else {
//alert("This website does not have the API")
}
}
function showError(error) {
switch (error.code) {
case error.PERMISSION_DENIED:
//alert("This website does not have permission to use the Geolocation API")
break;
case error.POSITION_UNAVAILABLE:
//alert("The current position could not be determined.")
break;
case error.TIMEOUT:
//alert("The current position could not be determined within the specified timeout period.")
break;
case error.UNKNOWN_ERROR:
//alert("The position could not be determined due to an unknown error.")
break;
}
}
function showMyPosition(position) {
// stick the position into a hidden form variable
var coordinates = position.coords.latitude + "," + position.coords.longitude;
//alert("The position is “ + coordinates;
}
In the above examples I have commented out the alerts which is analogous to messagebox.show() in Silverlight and Winforms projects, but the flow is pretty clear. In it you call getCurrentLocation(), in there the if else block checks to see if the navigator.geolocation object exists. If it does the browser supports the thing, and another function is called that actually points to either an error routine or the actual get the location routine. If it does not we fall through to the browser is old and crusty and you should update it.
This is all well and good except it still does not get the information into our Silverlight application.
Now from Silverlight I can call the javascript with a simple invocation
HtmlPage.Window.Invoke("getCurrentLocation");
This is all well and good except it still does not get the information into our Silverlight application.
That’s where the fun began yesterday with all this exercise in futility where attempting to get the results passed into Silverlight from the JavaScript call had me working up quite a sweat. Each trip into Google land yielding some other wonks demonstration of creating objects that marshal the inherent differences between JavaScript types and Silverlight types. All of these one failure after another. One of the primary problems is that the JavaScript and Silverlight threads are running apart from one another. This concurrency has the Silverlight call returning (with NULL) before the JavaScript engine can get the results. If you run the thing in debug and break on the JavaScript. You can see that it’s working. Yet at full speed no workie, frustrating as all hell.
At this point I started to just give up, but then I thought about how HCSIS (The PA abomination that one of our most successful products interfaces with) In this thing they use a host of HTML form level containers that are invisible to hold values that get passed into and out of processes. To that end I changed the ASPX page that instances the Silverlight plugin as shown below…
In the body segment at the bottom I added the bolded and enlarged line to create our form level hidden container
<body>
<form id="form1" runat="server" style="height:100%">
<input id="Hidden1" type="hidden" runat="server" value="0,0" />
<div id="silverlightControlHost">
<object data="data:application/x-silverlight-2,"
type="application/x-silverlight-2" width="1024" height="768" name="Xaml1">
<param name="source" value="ClientBin/IhSIS.xap"/>
<param name="windowless" value ="True" />
<param name="onError" value="onSilverlightError" />
<param name="background" value="Yellow" />
<param name="minRuntimeVersion" value="4.0.0.0" />
<param name="autoUpgrade" value="true" />
<param name="Initparams"
value="UserAccount=<%=HttpContext.Current.User.Identity.Name%>,
ipAddress=<%=HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]%>" />
<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0"
style="text-decoration:none">
<img src="http://go.microsoft.com/fwlink/?LinkId=161376"
alt="Get Microsoft Silverlight" style="border-style:none"/>
a>
object><iframe id="_sl_historyFrame"
style="visibility:hidden;height:0px;width:0px;border:0px">iframe>div>
form>
body>
Then up in script region I added my Java script code to get the results and place them into the hidden container. The error routine could be made a whole lot simpler as I am not really doing anything different for each of the 4 possible errors but I left it as is in case we wanted to handle them differently in the future. The basic procedure is we populate the hidden object with the string representation of the coordinates or 0,0 if there is some problem…
function getCurrentLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showMyPosition, showError);
}
else {
document.getElementById("Hidden1").value = "0,0";
}
}
function showError(error) {
switch (error.code) {
case error.PERMISSION_DENIED:
//alert("This website does not have permission to use the Geolocation API")
document.getElementById("Hidden1").value = "0,0";
break;
case error.POSITION_UNAVAILABLE:
//alert("The current position could not be determined.")
document.getElementById("Hidden1").value = "0,0";
break;
case error.TIMEOUT:
//alert("The current position could not be determined within the specified time out period.")
document.getElementById("Hidden1").value = "0,0";
break;
case error.UNKNOWN_ERROR:
//alert("The position could not be determined due to an unknown error.")
document.getElementById("Hidden1").value = "0,0";
break;
}
}
function showMyPosition(position) {
// stick the position into a hidden form variable
var coordinates = position.coords.latitude + "," + position.coords.longitude;
document.getElementById("Hidden1").value = coordinates;
}
So now I use the simple evocation early on in IntegraConnect’s startup of
HtmlPage.Window.Invoke("getCurrentLocation");
To populate the hidden object on our page with the location.
Then when I want to use it say off the geomap screen I can get it out of that hidden object with
using System.Windows.Browser;
HtmlElement elem = HtmlPage.Document.GetElementById("Hidden1");
if (elem != null)
{
string v = elem.GetAttribute("value");
MessageBox.Show(v);
}
In this case I am just showing the message box of the results but from here it’s easy to split the string on the , and get the Latitude and the Longitude to work with…
And the clouds parted, and the rays of sunshine rained down upon my keyboard, and the angelic chorus rose up... and all was good…
3 Comments:
CyrAhp [url=http://www.bootsmany.com/]ムートンブーツugg[/url] UkiPpr http://www.bootsmany.com/ GybJjk [url=http://www.bootsnewjp.com/]UGG アウトレット[/url] WwxOxd http://www.bootsnewjp.com/ RgcEmj [url=http://www.bootsbestjp.com/]ugg ブーツ[/url] GtvAeo http://www.bootsbestjp.com/ RslEfv[url=http://www.yasuibu-tsu.com/]ugg ブーツ[/url] SrlQml http://www.yasuibu-tsu.com/ AclOwz [url=http://www.yoibu-tsu.com/]アグ ブーツ[/url] YerVnf http://www.yoibu-tsu.com/ AorHxh [url=http://www.bootsjpsales.com/]ugg アグ ブーツ[/url] HspBfo http://www.bootsjpsales.com/ SzxRzi
9:28 AM
Now winter is in total swing you should have all the clothing which includes boots that will help you to stay warm. Though there are plenty of superb pairs of boots readily available that offer protection for your feet they don't really support to maintain them warm. If that you are interested, you can even be a kind of men and women.
You may essentially sit inside the comfort of the rather distinctive residence, produce a couple of clicks and watch for your get to have shipped for you. This does not imply although that obtaining on the net is regularly difficulty cost-no cost. These in essence investigation so genuine that customers would have a genuinely challenging time acquiring [url=http://www.vbboots.co.uk/ugg-boots-bailey-button-triplet-c-56.html]Ugg Boots Bailey Button Triplet[/url]
out just by in search of they are knock offs.
It really should come as no shock Uggs Online. What numerous action do men and women interact in that demands the identical http://www.vbboots.co.uk/ugg-boots-classic-tall-c-40.html
sorts of skills Shoes Uggs. It would will want triggered your body to burn up energy just before previously Shoes Uggs. But initial, [url=http://www.netsboot.com/ugg-boots-metallic-short-c-39.html]Ugg Boots Metallic Short[/url]
My companion and i brain straight to my personal tumble issues concerning 18 and also InStyle they will consistently supply amazing advice on the way to take with every single other rapid tumble classics. This sort of tumble, boot styles are becoming any quite hot admission merchandise. I adore to use my own cheap ugg boots designs.
For ladies who are dying to get dressed decently, black boots are surely the finest bet for expressive versatility. There is virtually no one could ever go wrong with wearing black boots. Naturally, black shades by no means fail to exude elegance and any other really feel you like.
2:07 PM
ykye [url=http://www.buy-canadagoose-sale.com]Canada Goose Jacket[/url] tywd http://www.buy-canadagoose-sale.com qolj pbtw [url=http://www.sale-canadagooseoutlet.com]Canada Goose Parka[/url] zogg http://www.sale-canadagooseoutlet.com hkoh rcjv [url=http://www.gocanadagoose-ca.com]Canada Goose Jacket[/url] henc http://www.gocanadagoose-ca.com mnka yugz [url=http://www.my-canadagooseoutlet.com]Canada Goose Jacket[/url] hbdm http://www.my-canadagooseoutlet.com oary vmqz [url=http://www.cheapbeatsbydreok.co.uk]Beats By Dre[/url] glrr http://www.cheapbeatsbydreok.co.uk qcuk
10:08 AM
Post a Comment
<< Home