Thursday, February 9, 2012

Google maps - code to display image from URL

protected void Page_Load(object sender, EventArgs e)

{

if (!(Page.IsPostBack))

{

// to get the image stream

//http://www.greywyvern.com/code/php/binary2base64

// google maps image

// http://code.google.com/apis/maps/documentation/staticmaps/

//// working url

////string GoogleURL = "http://maps.google.com/maps/api/staticmap?sensor=false&zoom=0&size=320x169&maptype=roadmap";

///////// ++++++++++++++++++++++++++++++++++++++

// working

//string GoogleURL = "http://maps.google.com/maps/api/staticmap?sensor=false&zoom=12&size=480x313&maptype=roadmap¢er=52.4871716,1.7153936&markers=52.4871716,1.7153936";

// not working

//string GoogleURL = "http://maps.google.com/maps/api/staticmap?sensor=false&zoom=12&size=480x313&maptype=roadmap¢er=51.5017821,-0.1154330&markers=51.5017821,-0.1154330";

// old

//string GoogleURL = "http://maps.google.com/maps/api/staticmap?sensor=false&zoom=0&size=320x169&maptype=roadmap";

string GoogleURL = "http://maps.google.com/maps/api/staticmap?sensor=false&zoom=12&size=480x313&maptype=roadmap¢er=-33.8389280,151.2085890&markers=-33.8389280,151.2085890&format=jpg";


/////////////////////////////////////string GoogleURL1 = HttpUtility.UrlDecode("http%3a%2f%2fmaps.google.com%2fmaps%2fapi%2fstaticmap%3fsensor%3dfalse%26zoom%3d0%26size%3d320x169%26maptype%3droadmap");

string strResponse = string.Empty;


WebRequest objWebRequest;

objWebRequest = WebRequest.Create(GoogleURL);

WebProxy proxy = new WebProxy();

objWebRequest.Proxy = proxy;

objWebRequest.UseDefaultCredentials = true;

objWebRequest.Proxy.Credentials = CredentialCache.DefaultCredentials;

objWebRequest.ContentType = "Content-Type: image/png";

StreamReader loResponseStream = new StreamReader(objWebRequest.GetResponse().GetResponseStream(), Encoding.UTF8);

var bytes = default(byte[]);

using (var memstream = new MemoryStream())

{

var buffer = new byte[512];

var bytesRead = default(int);

while ((bytesRead = loResponseStream.BaseStream.Read(buffer, 0, buffer.Length)) > 0)

{

memstream.Write(buffer, 0, bytesRead);

}

bytes = memstream.ToArray();

}

image1.Attributes["src"] = "data:image/png;base64," + Convert.ToBase64String(bytes);

//Response.Clear();

//Response.ContentType = "Image/png";

//Response.BinaryWrite(bytes);

}

}

1 comment:

  1. Hai,

    Nicely written post. I am just about to start a new blog and there could not have been a better guide than this one for some good pointers. I am looking forward to starting a successful blog after reading this incredibly useful post of yours. Waiting for more such posts like this.

    Keep writing and sharing!

    aamala

    SEO Training in Chennai

    ReplyDelete