Saturday, October 9, 2010

How to use Google Static Map API in Windows Mobile

I have noticed so many times over the Microsoft smart device forum lots of time developer ask this question to how to get Google static map according to latitude and longitude. So I have decided to write a very simple article with an example to solve this problem using some static geo data. Apart from that this article will cover, how to use Google Static Map API.

In this article I have used some static geo data like (latitude and longitude) to get the Google static map using google static map api. Here I am going to explain how to use google map api to get the google static map and how to pin location on the static map in widows mobile application.

Here we go.

Let us have a glace of Google static map documentation here

In this application I have created a GoogleMapAPIs class for format the Google Static Map API. This class contents two static methods for google map API. In these methods I have formed api for the generic purpose for passing parameters google map url.

A Google Static Maps API URL must be of the following form:

To get the static map API,75.8362304687&zoom=12&size=240x320&sensor=false

In the above url, some Map Parameters.

center: this parameter consider comma-separated {latitude,longitude} like this 25.1950004243,75.8362304687

zoom: this parameter we can pass the zoom level 0-21

size: this parameter we need to pass image size in height and width here I passed 240x320 in string format like width x height.

sensor: if the mobile application is using using a "sensor" then we have to pass true otherwise false|color:red|Delhi&sensor=false

In the above url, some Map Parameters.

size: this parameter we need to pass image size in height and width here I passed 240x320 in string format like width x height.

maptype:it isspecifies a standard type of map like roadmap, satellite, terrain, hybrid

markers:it is used to show pin on the map which is define one or more markers to attach to the image at specified locations.

sensor: if the mobile application is using a "sensor" then we have to pass true otherwise false

Let us take an example how to call Google map api in Windows mobile application.

In the windows mobile project I have added a class WebAccessUtility.cs this class have static method to call the google map api and download the image in Jpeg format.

public static bool GetStaticGoogleMap()
bool isSuccess = false;
string WebUrl = string.Empty;
if (isRoadMap)
WebUrl = GoogleMapAPIs.GetGoogleRoadMapAPIs();
WebUrl = GoogleMapAPIs.GetGoogleMapAPIs();

HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(WebUrl);
httpRequest.Timeout = 15000;
httpRequest.ReadWriteTimeout = 20000;

using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse())
using (Stream imageStream = httpResponse.GetResponseStream())
using (Bitmap buddyIcon = new Bitmap(imageStream))
buddyIcon.Save(strPath + strFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
isSuccess = true;

catch (WebException wx)
switch (wx.Status)
case WebExceptionStatus.ConnectFailure:
throw ex;
catch (Exception ex)
throw ex;
return isSuccess;

This method will take care to call the API type if user call the map type is road or normal. See below screen there is a menu you can choose location first then select the map type.

Now choose the Get Map from the Menu then it will display map accordingly.

To get the road type of map then chooses the Get Road Map from the Menu.

Download Source Code here

Note: do not forget to cradle your emulator or device for the internet connectivity.

Thank you :)


isuru said...

Can you please tell me what is the needed compact framework and is this supports to the windows mobile 5.0 to run this example

thank you!

AmitDiwan said...

hi sir,
Can OpenStreetView be implemented in silverligh(dotnet)...

I implemented BING MAPS in silverlight.
But having no idea about OpenStreetView.

Any help?

Raghavendra Rao I said...

Hai Dude,

I'm Raghavendra Rao, Software Developer working in NCPL Technologies in Visakhapatnam. I've been working here from past 2years. Did many of the projects in both windows and some on web. Here i've got a new problem in one of my project that has been assigned to me.On querying about the solution for it, i've came across your blog which felt very interesting and useful to me.

My problem is, I've to develop a GPS application in Windows Mobile. The reqts are as follows:

This entire application should be combined and should run in a windows mobile application.

Current Progress of my proj:

I've worked on the problem of my project and found out a solution. I've used Static Google map api's in order to find out the shortest path between two locations. Its working fine but a problem again arised. The points that i've selected are being displaced in the map but they are joined with a single straight line, but not navigating through the streets and all those areas. I've tried various methods to generate a path rather than a straight line, but failed.
Hence I request you to provide a quick and a useful solution for this prob