)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » StaticMap (Ia.Cl.Models.Google)

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: using System.Net;
    5: using System.Net.Http;
    6: using System.Text.Json;
    7:  
    8: namespace Ia.Cl.Models.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:              * appsettings.json:
  241:   "AppSettings": {
  242:     "GoogleRecaptchaSiteKey": "**********************",
  243:     "GoogleRecaptchaSecretKey": "**********************",
  244:   }
  245:             */
  246:  
  247:             //googleRecaptchaSiteKey = Ia.Cl.Models.ApplicationConfiguration.GetSetting("AppSettings:GoogleRecaptchaSiteKey");
  248:             googleRecaptchaSecretKey = Ia.Cl.Models.ApplicationConfiguration.GetSetting("AppSettings:GoogleRecaptchaSecretKey");
  249:  
  250:             using (var httpClientHandler = new HttpClientHandler())
  251:             {
  252:                 // https://blog.roushtech.net/2016/12/20/ignoring-ssl-certificate-errors-net-core-httpclient/
  253:                 httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };
  254:  
  255:                 using (var httpClient = new HttpClient(httpClientHandler))
  256:                 {
  257:                     var res = httpClient.GetAsync($"https://www.google.com/recaptcha/api/siteverify?secret=" + googleRecaptchaSecretKey + $"&response={gRecaptchaResponse}").Result;
  258:  
  259:                     if (res.StatusCode != HttpStatusCode.OK) b = false;
  260:                     else
  261:                     {
  262:                         string JSONres = res.Content.ReadAsStringAsync().Result;
  263:  
  264:                         using (var jsonDocument = JsonDocument.Parse(JSONres))
  265:                         {
  266:                             JsonElement success = jsonDocument.RootElement.GetProperty("success");
  267:  
  268:                             b = success.GetBoolean(); // jsonDocument.su.success == "true";
  269:                         }
  270:                     }
  271:                 }
  272:             }
  273:  
  274:             return b;
  275:         }
  276:     }
  277:  
  278:     ////////////////////////////////////////////////////////////////////////////
  279:  
  280:     /// <summary>
  281:     /// Generic helper functions
  282:     /// </summary>
  283:     public class Tools
  284:     {
  285:         ////////////////////////////////////////////////////////////////////////////
  286:  
  287:         /// <summary>
  288:         /// Converts Integers to enum types
  289:         /// </summary>
  290:         /// <typeparam name="T"></typeparam>
  291:         /// <param name="value">Enum int value</param>
  292:         /// <returns></returns>
  293:         /// <example>
  294:         /// Enums.ConvertToEnum enum.type ([EnumAsInt]);
  295:         /// </example>
  296:         public static T ConvertToEnum<T>(int value)
  297:         {
  298:             return (T)Enum.ToObject(typeof(T), value);
  299:         }
  300:  
  301:         ////////////////////////////////////////////////////////////////////////////
  302:  
  303:         /// <summary>
  304:         /// Converts String to enum types
  305:         /// </summary>
  306:         /// <typeparam name="T"></typeparam>
  307:         /// <param name="value">Enum string value</param>
  308:         /// <returns></returns>
  309:         /// <example>
  310:         /// Enums.ConvertToEnum([EnumAsString]);
  311:         /// </example>
  312:  
  313:         public static T ConvertToEnum<T>(string value)
  314:         {
  315:             return (T)Enum.Parse(typeof(T), value, true);
  316:         }
  317:  
  318:         ////////////////////////////////////////////////////////////////////////////
  319:         ////////////////////////////////////////////////////////////////////////////
  320:     }
  321: }