Locate Me and Virtual Earth Sample for Silverlight

A little sample app that uses Silverlight 3, the Virtual Earth Map Control CTP and IP-based geo-coding to implement the Locate Me scenario...

One of the services available to apps in the mobile world is location, and it is interesting to see all sorts of iPhone apps use it effectively in compelling ways. However, I tend to think that you want browser and desktop apps to have a notion of location as well, and be able to use a somewhat accurate guess, even if it isn’t precise. Location, location, location... right?

To check out whats possible with IP-based location services, I wrote a little Locate Me type of sample that uses the IP address of the client machine using a service such as HostIP.info (you can contribute location and help build their db, if your IP can't be resolved). And then I decided to use this as an excuse to play with the Virtual Earth CTP for Silverlight.

Sitting in my office, when I make a request to http://api.hostip.info/get_html.php?position=true, I get the following response:

Country: UNITED STATES (US)
City: Redmond, WA
Latitude: 47.6742
Longitude: -122.115

While you can also optionally provide an IP address to the service, by default it uses the client’s IP address, which is just what I want. Thankfully, HostIP supports cross-domain access, and that makes things just click. I can have my client code determine latitude and longitude, and place a pushpin on the map.

You can check out the sample code, which is pretty small and self-explanatory (a map control, a GeoLocation service which encapsulates the HostIP service), and you can also check out the live sample, and see how accurate the IP-based lookup is for your own location. Any other services similar to HostIP.info that you've used successfully? I'd love to hear...

Update: - Tim Heuer sent me an updated sample that uses Google's Ajax APIs to determine location, which might have overall better coverage (thanks Tim). Code and live sample updated.

Posted on Thursday, 4/30/2009 @ 9:05 PM | #Silverlight


Comments

21 comments have been posted.

Kevin

Posted on 4/30/2009 @ 11:15 PM
Hey,

Pretty cool app, I came curious though, when you right click the map and get an option to "Install Locate Me on this computer..." is that something you programmed into your app or is it a default offering of Silverlight 3.0. Unfortunately the out-of-browser bit isn't working for me but otherwise I like very much.

Cheers,
K

Nikhil Kothari

Posted on 4/30/2009 @ 11:37 PM
@Kevin - hmmm... I was experimenting with out-of-browser, seems like I forgot to remove that bit from my manifest before uploading. For some reason the map control doesn't work out-of-browser... need to figure out why.

Also, Tim Heuer sent me an updated sample that uses the Google APIs for location - will try to get that uploaded, too once I get it working.

Pure Krome

Posted on 5/1/2009 @ 1:12 AM
IPAddressExtensions @ Codeplex (http://ipaddressextensions.codeplex.com/) also offers the same functionality as HostIP.info, without having to make a roundtrip to their servers. Just embed the DLL in your app :)

(Disclaimer/Transparency: I'm the author of that .dll, btw)

Nikhil Kothari

Posted on 5/1/2009 @ 1:25 AM
@Pure - Correct me if I am wrong, but that doesn't give you lat/lon and since you don't have a service dependency, you also have to package the mapping data in your app, which is probably something you want to avoid - both because it might go out of date, and because it might be too large to embed in your xap...

punkcoder

Posted on 5/1/2009 @ 4:00 AM
I can't get this to work on my Mac in Safari, is the new Silverlight not available for Mac yet? When I visit the sample page it indicates I need a newer version of Silverlight. If I click on get newer version it downloads the Silverlight 2 plugin.

Mark Richards

Posted on 5/1/2009 @ 4:52 AM
Punkcoder, you need to install SL3 for the mac. Check here: http://silverlight.net/getstarted/silverlight3/default.aspx

Marti

Posted on 5/1/2009 @ 5:21 AM
HostIP.info isn't great for me, it reports that I'm in Brighton (England) when I'm actually in Ipswich (England) that's ~120 miles away.

Check out my VE demo at http://martimedia.blogspot.com/ of UK MP expesnes (~600 push pins).

Pure Krome

Posted on 5/1/2009 @ 6:18 AM
@Nikhil - That is correct, IPAddressExtensions does not give any Lat/Long. I missed that you were getting that back from HostIP.info. It only returns the country. An example of this might be to localise the site based on IP. To do that, I've gzip'd a csv of the IP blocks and how they have been assigned to which country. The dll is only a few hundred k in size ('cause of the compressed csv). That's not too bad, IMO. It will definately date though, but for sites that receive heavy traffic, that might be a little bit of a saviour as it reduces the round trips required for the first lookup.

But yeah, Lat/Long results are waaaaay better :)


@Marti - that is because the data for the closest router on your ip traceroute (which exists in their database) is at Brighton. Could that be the closest Point of Presence (POP) for your ISP? As Nikhil said, this is why people can upload content to their DB to help fix their listings to make it more accurate. HTH.

Marti

Posted on 5/1/2009 @ 6:53 AM
@Pure - I don't think it's the ISP PoP, as other commerical IP sites (e.g. http://www.maxmind.com/app/mylocation) do resolve the address to Ipswich, I guess it's just the limited dataset for the free HostIP.info service.

Marti

Posted on 5/1/2009 @ 7:03 AM
Anyone tried this free C# API to binary IP dataset http://www.maxmind.com/app/csharp, or found a public webservice interfacing to this data?

Max

Posted on 5/1/2009 @ 1:08 PM
Your live example is asking me install Silverlight. Thought I aleardy had Silverlight, I installed the latest version. It did not work, every time I click the example link, it prompts me to install Silverlight. I have uninstalled and installed Silverlight twice but nothing works.

Nikhil Kothari

Posted on 5/1/2009 @ 1:42 PM
@Max - the sample requires the Silverlight 3. See http://silverlight.net/getstarted/silverlight3/default.aspx to get started.

John

Posted on 5/1/2009 @ 5:38 PM
Why did you use Silverlight 3 not 2? The Virtual Earth control is SL2, I'm not sure what feature of SL3 is needed for this, could this not be done in SL2?
Reading the comments it looks like you were playing with out of browser, fair enough.

punkcoder

Posted on 5/1/2009 @ 7:00 PM
@Mark Richards

Thanks, that fixed my issue on the Mac.

Brian

Posted on 5/2/2009 @ 12:35 PM
We did something similar for www.myworldmaps.net called Worldmaps Stumbler, that tracks website visitors in near-real time:

http://www.myworldmaps.net/sl/WorldmapsStumbler.aspx

Sandeep Aparajit

Posted on 5/4/2009 @ 8:44 PM
Nice application Nikhil.
For mobile phone's it is very obvious to get more accurate location due to the transmission tower locations. But when coming to computer's it's not accurate since we often connect to some remote networks and then perform our job. For example, I was sitting in India and tried this application. It gave me the same location as yours since my a/c is in Redmond. Well, the GPS kind of apps for computers are new and would definitely evolve to have more accurate locations. Thanks for your article!

Jose Madriz

Posted on 5/12/2009 @ 10:13 AM
Guys, this map app moves incredibly.. this demo should be running some cool overlay information to show the full potential of SL3.. any ideas around.. some viral mashup to show off this really great performing map app?

Nikhil Kothari

Posted on 5/12/2009 @ 9:57 PM
@Jose - Indeed... I have an app idea I want to work on... right after I finish this RIA Services sample I am working on right now. :-)

Steve Strong

Posted on 6/3/2009 @ 4:55 PM
I like this, it has inspired me to start a series on silverlight virtual earth control

Steve Strong

Posted on 6/3/2009 @ 4:57 PM
www silverenlightenment com

Gurp

Posted on 6/13/2009 @ 3:02 AM
this is the best
Post your comment and continue the discussion.