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