Locating Windows Mobile phone without GPS
We all know apps like Google Maps Mobile, Microsoft’s Bing equivalent, Nokia Maps, etc. In every app you have option to locate yourself, even if you don’t have GPS or it’s turned off. The only thing you need is quite good signal and access to Internet.
Obvious question is: how is it done? Since I was playing a little bit with Windows Mobile, I’ll show you how to create simple app, which will show your current location. OK, not your exact location, but location of the closest Cell ID, which is in cities quite accurate (on few 100 meters).
This app is capable of showing Cell ID info, “transforming” it into latitude and longitude, finding the closest address and showing map of your location. To achieve this, I have used a few services, that are out there:
- First of all cool wrapper for Radio Interface Layer (RIL) written in C# by Dale Lane
- Mobile Location – web service for “transforming” Cell ID into latitude and longitude by Ericsson Labs – suppose to have 3.5million cells all around the world in their databases!
- Bing Maps API – web service by Microsoft for everything you want to do with maps
Everything is very well documented, so we just need to put everything together.
The first step is very easy – just download RIL.cs from Dale’s blog and include it into your Windows Mobile project. For showing up the Cell info, you just need to modify this function rilResultCallback in RIL.cs, because original function is lacking MNC (Mobile Network Code) data, so we will add this (bolded):
public static void rilResultCallback(uint dwCode, IntPtr hrCmdID, IntPtr lpData, uint cbData, uint dwParam) { //.... celltowerinfo = rilCellTowerInfo.dwCellID + "-" + rilCellTowerInfo.dwLocationAreaCode + "-" + rilCellTowerInfo.dwMobileNetworkCode + "-" + rilCellTowerInfo.dwMobileCountryCode; //... }
Before we will pass this data to Mobile Location service, we need to register ourselves for API key. It’s simple, just fill in the form and you’ll get API key. So, next thing is to pass Cell ID info into this web service. We have option of choosing JSON or XML version, and I have chosen JSON version (with no obvious reason), so I needed to somehow parse JSON into more usable form. For doing that, I used code, found in this blog post. Remember, we are on mobile device, so using very light libraries is essential, since those devices are still not so powerful. Also notice, to put your API key instead of “yourkey” string into web service url.
string cellInfoFull = RIL.GetCellTowerInfo(); string[] cellInfo = cellInfoFull.Split(new char[] { '-' }); cellInfo[0] = Convert.ToInt64(cellInfo[0]).ToString("X"); cellInfo[1] = Convert.ToInt64(cellInfo[1]).ToString("X"); WebRequest oRequest = WebRequest.Create(
String.Format("http://cellid.labs.ericsson.net/json/lookup
?cellid={0}&mnc={2}&mcc={3}&lac={1}&key=yourkey",cellInfo)); WebResponse oResponse = oRequest.GetResponse(); StreamReader oReader = new StreamReader(oResponse.GetResponseStream()); string sContent = oReader.ReadToEnd(); Hashtable h = (Hashtable)JsonParser.JSON.JsonDecode(sContent); string latlong = ((Hashtable)h["position"])["latitude"].ToString() +
"," + ((Hashtable)h["position"])["longitude"].ToString();
We could finish here, but let me just add a little map into our application. For doing that, we need another API key, this time from Microsoft – find it here. Again, very easy, just filling in form and you’re done.
For usage and very cool example (with all steps how to use this service) follow this link on MSDN, so I will not explain in detail how to use it. We will just copy two functions into our project (after we have added two web services – GeocodeService and ImageryService) - ReverseGeocodePoint(string locationString) and GetImagery(string locationString). The first function is ready to use:
textBox2.Text = latlong + "\n" + ReverseGeocodePoint(latlong);
pictureBox1.Image = GetImageFromUrl(GetImagery(latlong));
What we’ve done here is to “convert” latitude and longitude into actual closest address.
For the second we have to add function for downloading image from given URL, which is pretty straight forward:
private Bitmap GetImageFromUrl(string url) { HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url); myRequest.Method = "GET"; HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); System.Drawing.Bitmap bmp =So that’s it! Now you need to create cool UI and a little bit of try-catch stuff :)
new System.Drawing.Bitmap(myResponse.GetResponseStream()); return bmp; }
Sure, this method will not work all around the world and is not accurate, so you need to do extensive testing, if you have plans to implement that kind of solution into your application.
What you can do to improve this method is to implement so called triangulation – basic idea is, that (especially) in urban areas, there are multiple cells, that covers each other. By tracking movements of your phone you can locate it much more accurate. I will finish here, since that would be completely out of scope, so leave a comment if you have any questions!
| Tweet |
author: Aleš Rosina | Comments: 6 | Tags: windows-mobile, location, ril, c-sharp
January
2010
Thanks this really helps me :-)
Nice Work
Hi,
unfortunatelly, it doesn't work :(
Could you please send workable source?
Thx!
Now a day’s mobile phones had become the part and parcel of the life. Thus everyone wants to locate themselves on this devices but most of the people thinks it is possible only when they have GPS service activated. But this post is very useful to those people who don’t have GPS facility on their mobile. Your explanation very clear and attractive it is understandable by even a layman I appreciate your way of explanation. Thanks a lot for sharing this information with us.
Hi,
unfortunatelly, it doesn't work :(
Could you please send workable source?
Thx!
Thanks for sharing this knowledge.. I would try this out..
Search Marketing solutions to UK businesses looking to promote their websites on the internet and through the search engines.

![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=1761bee7-5903-4975-92c8-39b39a38aa9c)
