Google Maps - Get Map Position using GPS

2012-03-23 by

This post will show you how to gather some information using the GPS API from the Windows Mobile device.  This will then allow a user to download the 640x640 map from Google's Static Map Service with the center location as the current position of the GPS Device.  This is an interim solution until Google Maps supports the ability to use the Map service without Javascript having to be enabled.



To download static maps from google you will need to do the following

  1. Get you Google Maps API Developer Key from Google's API Center
    • The key provided will be used to replace GOOGLE_API_KEY in all of the sample code
  2. Create a class that will construct a Google Maps Static Map Call for you
  3. Download the Map using the HTTP Handler
  4. Return the bitmap to the application

Google Maps - Static Map Builder Class

using System;
using System.Globalization;
using System.Net;
using System.Drawing;
using System.Collections.Generic;

//This code is free to use in non commercial applications
//Copyright Joshua bylotas 2009
namespace GoogleMapsSample
{
    class StaticMapGetter
    {
 
        private string GetURL(double latitude, double longitude, int zoomLevel, string mapType, string apiKey)
        {
            return GetURL(latitude, longitude, zoomLevel, mapType, apiKey, 640, 640);
        }
        private string GetURL(double latitude, double longitude, int zoomLevel, string mapType, string apiKey, int width, int height)
        {
            string retVal = String.Format(CultureInfo.InvariantCulture, "http://maps.google.com/staticmap?center={0},{1}&size={5}x{6}&zoom={2}&maptype={3}&key={4}",
                  latitude, longitude, zoomLevel, mapType, apiKey, width, height);
            return retVal;
        }
 
        public System.Drawing.Bitmap GetMap(double latitude, double longitude, int zoomLevel, string mapType, string apiKey)
        {
            WebRequest request = HttpWebRequest.Create(GetURL(latitude,longitude,zoomLevel,mapType,apiKey));
            WebResponse response = request.GetResponse();
            Bitmap bmp = new Bitmap(response.GetResponseStream());
            return bmp;
        }
    }
}
 
Google Maps Sample Code

 GoogleMapsSample.StaticMapGetter getter = new GoogleMapsSample.StaticMapGetter();
 

Bitmap bmpBackImage = getter.GetMap(position.Latitude, position.Longitude, 19, "satellite", "GOOGLE_API_KEY");