Search

News Update :
Powered by Blogger.

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:


http://maps.google.com/maps/api/staticmap?parameters


To get the static map API



http://maps.google.com/maps/api/staticmap?center=25.1950004243,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


http://maps.google.com/maps/api/staticmap?size=240x290&maptype=roadmap&markers=size:mid|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;
try
{
if (isRoadMap)
{
WebUrl = GoogleMapAPIs.GetGoogleRoadMapAPIs();
}
else
{
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;
break;
}
}
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 :)


komentar | | Read More...

Total Pageviews

Followers

 
Design Template by panjz-online | Support by creating website | Powered by Blogger