Monday, December 29, 2008

A static Google Maps proxy in C#

Following on from this blog post - http://hasin.wordpress.com/2008/10/31/wrapper-for-google-static-map-api/ - I thought I'd have a go at my own proxy for Google static Maps in C#

This simple HTTP Handler is the core of what I wrote.

The reason I wrote it.... facebook doesn't seem to like talking to googlemaps directly.

The potential problem.... I think this falls completely within their guidelines (I am still using google maps within a web page), but I think my website could easily overreach their loading quotas - a limit of 1000 requests per user per day (and I think my website proxy will count as one user?)

I'll see how it goes....



using System;

using System.Web;

using System.Net;

using System.IO;

 

namespace MapControl

{

    public class GMapProxy : IHttpHandler {

 

        static void CopyStream(Stream input, Stream output)

        {

            byte[] buffer = new byte[0x1000];

            int read;

            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)

                output.Write(buffer, 0, read);

        }

          

        public void ProcessRequest (HttpContext context)

        {

            string gmapUri = string.Format("http://maps.google.com/staticmap{0}", context.Request.Url.Query);

            WebRequest request = WebRequest.Create(gmapUri);

 

            using (WebResponse response = request.GetResponse())

            {

                context.Response.ContentType = response.ContentType;

                Stream responseStream = response.GetResponseStream();

               

                CopyStream(responseStream,context.Response.OutputStream);

            }

        }

    

        public bool IsReusable {

            get {

                return false;

            }

        }

    }

}


No comments:

Post a Comment