Google Maps API on Windows Mobile

0.00 avg. rating (0% score) - 0 votes

Although Google Maps client for Windows Mobile and the Google Maps API have long been available, there have been few samples to use the Google Maps API on a Windows Mobile phone. This is because Google Maps API is available in Javascipt and meant for websites and its terms of service only allow using the API on websites, and not applications.

Getting Google Maps API to work on Windows Mobile

For a .NET application, possible workarounds (despite violating the TOS) including using the Json.NET library to execute JavaScript from the application and parse the response from google. However, this is not feasible for a Windows Mobile application since there is currently no versions of Json.NET available for the .NET Compact Framework, and porting the library is a difficult task.

Luckily somebody has done some reverse-engineering on the Google Maps API and made available the observations here. The ideas is that, all Google Maps API functions post their request to http://maps.google.com/maps, and if the request format is known, we can directly call the API from our application using HttpWebRequest class, without having to resort to Json.NET library.

Retrieving your approximate location

The original web page has long gone, but luckily web.archive.org keeps a copy of it. Among the samples, the most interesting one features the ability to get your (approximate) location (e.g. longitude and latitude. a.k.a geocode) from the cells that your mobile phone is currently connected to. Precisely, the application needs to provide the Mobile Country Code (MCC, 525 for Singapore), Mobile Network Code (MNC, 01 for Singtel), Location Area Code (LAC), and Cell-ID and the API will return the approximate geocode.

On a Windows Mobile device, the MCC and the MNC can be retrieved either using lineGetCurrentOperator from Telephony API (TAPI) or RIL_GetCellTowerInfo from Radio Interface Layer (RIL). The other 2 parameters (LAC, CID) can only be retrieved via RIL. Notice that since implementations of TAPI and RIL are OEM-dependent, on some phones you can use both API, on other phones you can use one but not the other and some phones will prohibit access to both APIs.

Getting the address of a location

I improved the sample code further by adding the ability to display the address resolved from geocode. The approach is straightforward by passing the geocode to the following Google URL and passing the response:

http://maps.google.com/maps?output=json&f=q&hl=en&q=103,2&ie=UTF8&z=15

where 103 and 2 are the latitude and longitude respectively. The output=json parameter tells Google to return the output in simple text format, which can be read by your code; without it you’ll get a full Google Maps web page.

Getting nearby locations and their addresses

When you use Google Maps’ My Location feature with no GPS, you’ll get a message like “Your location within 300 meteres”. The accuracy (e.g. 300 meters in this case) is calculated automatically based on the cells you are connected to, the neighbouring cells and perhaps the phone signal strength. There is no documented algorithm to calculate this, and the above API also does not return this parameter.

To cater for this, you can get a list of nearby locations within x km of the geocode returned by Google by using the following code snippet to query all the neighbouring geocodes:

double longLatInterval = 0.02;
double longLatDelta = 0.005;
double maxDistance = 1; //get all locations within the specified distance, in km
for (double curLong = lon – longLatDelta; curLong < lon + longLatInterval; curLong += longLatDelta)
for (double curLat = lat – longLatDelta; curLat < lat + longLatInterval; curLat += longLatDelta)
{
double dist = distance(curLat, curLong, lat, lon, ‘K’);

if (dist < maxDistance)
{
//found a possible nearby location
}
}

where (lon, lat) is your current geocode. You can get the function to calculate the distance between 2 geocodes from here.

The full sample source code can be downloaded here. It is an Windows Mobile 6 console application written in C# that simply displays the addresses of nearby locations and then quits.

0.00 avg. rating (0% score) - 0 votes
ToughDev

ToughDev

A tough developer who likes to work on just about anything, from software development to electronics, and share his knowledge with the rest of the world.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>