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

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