Wednesday 21 June 2017

Google map convert as a image or byte in C#

C# - Class file:

public class GoogleMapHelper
    {
        public static byte[] GetGoogleMapLocationAsByte(double lattitude,double longititude)
        {
         
            byte[] mapa;
            try
            {
           
         
                string url = @"http://maps.googleapis.com/maps/api/staticmap?center=" + lattitude + "," + longititude +
                    "&zoom=15&size=504x400&maptype=roadmap&markers=color:red%7Clabel:%7C" + lattitude + "," + longititude + "&sensor=false";

                using (WebClient wc = new WebClient())
                {
                   return mapa = wc.DownloadData(url);
                }
            }
            catch
            {
                return  new byte[]{};
            }
        }
    }


Cshtml

@{
    var base64 = Convert.ToBase64String(
GoogleMapHelper.GetGoogleMapLocationAsByte(11.0420371356699,77.0446100099899) );
    var imgSrc = String.Format("data:image/gif;base64,{0}", base64);
}


<img src="@imgSrc" />