2011年4月7日 星期四

C# .Net 與 Google Map 靜態地圖擷取

string strResult = "fail";
FileStream fileStream = null;
//設定圖片長寛
int mapWidth = Convert.ToInt16(System.Configuration.ConfigurationManager.AppSettings["MapImageWidth"]);
int mapHeight = Convert.ToInt16(System.Configuration.ConfigurationManager.AppSettings["MapImageHeight"]);
//設定GoogleMapKey
string apikey = System.Configuration.ConfigurationManager.AppSettings["GoogleMapKey"].ToString();
string url = "http://maps.google.com.tw/staticmap?center=" + Latitude + "," + Longitude + "&zoom=16&size=" + mapWidth.ToString() + "x" + mapHeight.ToString() + "&maptype=roadmap&markers=" + Latitude + "," + Longitude + "&key=" + apikey + "&sensor=false";
WebRequest myrequest = WebRequest.Create(url);
WebResponse mywebresponse = myrequest.GetResponse();
//讀取地圖靜態圖片資料
Stream mystream = mywebresponse.GetResponseStream();

string gifFile = string.Format("{0}\\{1}\\{2}\\Map.gif", System.Configuration.ConfigurationManager.AppSettings["ImagesFolder"], StoreID, ObjectID);

if (!System.IO.File.Exists(gifFile))
{
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(gifFile));
}

fileStream = File.Create(gifFile);
byte[] buffer = new byte[102400];
int bytesRead;
while (true)
{
//開始將資料流寫入到disk
bytesRead = mystream.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
break;
fileStream.Write(buffer, 0, bytesRead);
strResult = "OK";
}

沒有留言:

張貼留言