شركة التطبيقات المتكاملة لتصميم النظم البرمجية الخاصة

Integrated Applications Programming Company

Skip Navigation LinksHome » Code Library » StaticMap

Public general use code classes and xml files that we've compiled and used over the years:

Google support class.

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Configuration;
   4:   
   5:  namespace Ia.Cl.Model.Google
   6:  {
   7:      /// <summary publish="true">
   8:      /// Google support class.
   9:      /// </summary>
  10:      /// <value>
  11:      /// Class to generate a static map using the Google StaticMaps API
  12:      /// https://developers.google.com/maps/
  13:      /// </value>
  14:      /// <remarks> 
  15:      /// Copyright � 2001-2016 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
  16:      ///
  17:      /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
  18:      /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  19:      ///
  20:      /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  21:      /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  22:      /// 
  23:      /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
  24:      /// 
  25:      /// Copyright notice: This notice may not be removed or altered from any source distribution.
  26:      /// </remarks> 
  27:      public class StaticMap
  28:      {
  29:          /// <summary/>
  30:          public Double LatitudeCenter { get; set; }
  31:          /// <summary/>
  32:          public Double LongitudeCenter { get; set; }
  33:          /// <summary/>
  34:          public int Zoom { get; set; }
  35:          /// <summary/>
  36:          public int Width { get; set; }
  37:   
  38:          /// <summary/>
  39:          public int Height { get; set; }
  40:   
  41:          ////////////////////////////////////////////////////////////////////////////
  42:   
  43:          /// <summary>
  44:          /// Renders an image for display
  45:          /// </summary>
  46:          public string Render()
  47:          {
  48:              string s = "https://maps.googleapis.com/maps/api/staticmap?center={0},{1}&zoom={2}&size={3}x{4}&maptype={5}";
  49:   
  50:              string maskerString = "";
  51:   
  52:              s = string.Format(s, LatitudeCenter, LongitudeCenter, Zoom, Width, Height, Type.ToString().ToLower());
  53:   
  54:              // add markers
  55:              foreach (var marker in markerList)
  56:              {
  57:                  maskerString += string.Format("{0},{1},{2}|", marker.Latitude, marker.Longitude, GetMarkerParams(marker.Size, marker.Color, marker.Character));
  58:              }
  59:   
  60:              if (maskerString.Length > 0)
  61:              {
  62:                  s += "&markers=" + maskerString.Substring(0, (maskerString.Length - 1));
  63:              }
  64:   
  65:              s += "&key=" + ApiKey;
  66:   
  67:              return s;
  68:          }
  69:   
  70:          ////////////////////////////////////////////////////////////////////////////
  71:   
  72:          /// <summary>
  73:          /// Build the correct string for marker parameters
  74:          /// </summary>
  75:          private static string GetMarkerParams(mSize size, mColor color, string character)
  76:          {
  77:              string marker;
  78:   
  79:              // check if can render character
  80:              if ((size == mSize.Normal) || (size == mSize.Mid))
  81:              {
  82:                  if (size == mSize.Normal) marker = color.ToString().ToLower() + character;
  83:                  else marker = size.ToString().ToLower() + color.ToString().ToLower() + character;
  84:              }
  85:              else
  86:              {
  87:                  // just render size and color - character not supported
  88:                  marker = size.ToString().ToLower() + color.ToString().ToLower();
  89:              }
  90:   
  91:              return marker;
  92:          }
  93:   
  94:          ////////////////////////////////////////////////////////////////////////////
  95:   
  96:          /// <summary>
  97:          /// Defines a single map point to be added to a map
  98:          /// </summary>
  99:          public class Marker
 100:          {
 101:              private string _char = "";
 102:   
 103:              /// <summary/>
 104:              public Double Latitude { get; set; }
 105:   
 106:              /// <summary/>
 107:              public Double Longitude { get; set; }
 108:   
 109:              /// <summary/>
 110:              public StaticMap.mSize Size { get; set; }
 111:   
 112:              /// <summary/>
 113:              public StaticMap.mColor Color { get; set; }
 114:   
 115:              /// <summary>
 116:              /// Optional single letter character
 117:              /// </summary>
 118:              public string Character
 119:              {
 120:                  get { return _char; }
 121:                  set { _char = value; }
 122:              }
 123:          }
 124:   
 125:          /// <summary/>
 126:          public enum mFormat
 127:          {
 128:              /// <summary/>
 129:              gif = 0,
 130:              /// <summary/>
 131:              jpg = 1,
 132:              /// <summary/>
 133:              png = 2
 134:          }
 135:   
 136:          /// <summary/>
 137:          public enum mSize
 138:          {
 139:              /// <summary/>
 140:              Normal = 0,
 141:              /// <summary/>
 142:              Mid = 1,
 143:              /// <summary/>
 144:              Small = 2,
 145:              /// <summary/>
 146:              Tiny = 3
 147:          }
 148:   
 149:          /// <summary/>
 150:          public enum mColor
 151:          {
 152:              /// <summary/>
 153:              Black = 0,
 154:              /// <summary/>
 155:              Brown = 1,
 156:              /// <summary/>
 157:              Green = 2,
 158:              /// <summary/>
 159:              Purple = 3,
 160:              /// <summary/>
 161:              Yellow = 4,
 162:              /// <summary/>
 163:              Blue = 5,
 164:              /// <summary/>
 165:              Gray = 6,
 166:              /// <summary/>
 167:              Orange = 7,
 168:              /// <summary/>
 169:              Red = 8,
 170:              /// <summary/>
 171:              White = 9
 172:          }
 173:   
 174:          /// <summary/>
 175:          public enum mType
 176:          {
 177:              /// <summary/>
 178:              Roadmap = 0,
 179:   
 180:              /// <summary/>
 181:              Mobile = 1
 182:          }
 183:   
 184:          /// StaticMap props
 185:   
 186:          private List<Marker> markerList = new List<Marker>();
 187:          private StaticMap.mType _type = StaticMap.mType.Roadmap;
 188:   
 189:          /// <summary>
 190:          /// List of all markers to be displayed on the map
 191:          /// </summary>
 192:          public List<Marker> Markers
 193:          {
 194:              get { return markerList; }
 195:              set { markerList = value; }
 196:          }
 197:   
 198:          /// <summary/>
 199:          public StaticMap.mType Type
 200:          {
 201:              get { return _type; }
 202:              set { _type = value; }
 203:          }
 204:   
 205:          /// <summary>
 206:          /// Google maps API key - required!
 207:          /// </summary>
 208:          public static string ApiKey
 209:          {
 210:              get
 211:              {
 212:                  return ConfigurationManager.AppSettings["GoogleApiKey"]; ;
 213:              }
 214:          }
 215:      }
 216:   
 217:   
 218:      ////////////////////////////////////////////////////////////////////////////
 219:   
 220:      /// <summary>
 221:      /// Generic helper functions
 222:      /// </summary>
 223:      public class Tools
 224:      {
 225:          ////////////////////////////////////////////////////////////////////////////
 226:   
 227:          /// <summary>
 228:          /// Converts Integers to enum types
 229:          /// </summary>
 230:          /// <typeparam name="T"></typeparam>
 231:          /// <param name="value">Enum int value</param>
 232:          /// <returns></returns>
 233:          /// <example>
 234:          /// Enums.ConvertToEnum enum.type ([EnumAsInt]);
 235:          /// </example>
 236:          public static T ConvertToEnum<T>(int value)
 237:          {
 238:              return (T)Enum.ToObject(typeof(T), value);
 239:          }
 240:   
 241:          ////////////////////////////////////////////////////////////////////////////
 242:   
 243:          /// <summary>
 244:          /// Converts String to enum types
 245:          /// </summary>
 246:          /// <typeparam name="T"></typeparam>
 247:          /// <param name="value">Enum string value</param>
 248:          /// <returns></returns>
 249:          /// <example>
 250:          /// Enums.ConvertToEnum([EnumAsString]);
 251:          /// </example>
 252:   
 253:          public static T ConvertToEnum<T>(string value)
 254:          {
 255:              return (T)Enum.Parse(typeof(T), value, true);
 256:          }
 257:   
 258:          ////////////////////////////////////////////////////////////////////////////
 259:          ////////////////////////////////////////////////////////////////////////////
 260:      }
 261:  }