PageRenderTime 95ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/mojoPortal.Web.Controls/google/GMapBasePanel.cs

#
C# | 176 lines | 101 code | 31 blank | 44 comment | 5 complexity | a05f60f1ea0cdd58de6620112c1c493f MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause, CPL-1.0, CC-BY-SA-3.0, GPL-2.0
  1. /// Author: Joe Audette
  2. /// Created: 2007-12-04
  3. /// Last Modified: 2007-12-05
  4. ///
  5. /// The use and distribution terms for this software are covered by the
  6. /// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
  7. /// which can be found in the file CPL.TXT at the root of this distribution.
  8. /// By using this software in any fashion, you are agreeing to be bound by
  9. /// the terms of this license.
  10. ///
  11. /// You must not remove this notice, or any other, from this software.
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Configuration;
  15. using System.Globalization;
  16. using System.Text;
  17. using System.Web.UI;
  18. using System.Web.UI.WebControls;
  19. namespace mojoPortal.Web.Controls.google
  20. {
  21. public class GMapBasePanel : Panel
  22. {
  23. private string googleJsBasePath = "~/ClientScript/google/";
  24. private string gMapApiKey = string.Empty;
  25. private string gMapWebConfigKey = "GoogleMapsAPIKey";
  26. private bool apiKeyFound = false;
  27. private MapType gmapType = MapType.G_NORMAL_MAP;
  28. private int zoomLevel = 13; //default is 13
  29. private string noApiKeyWarning = "Could not find Google Maps API Key in Site Settings or Web.config";
  30. private string protocol = "http";
  31. //const string MapTypeNormal = "G_NORMAL_MAP";
  32. //const string MapTypeSatellite = "G_SATELLITE_MAP";
  33. //const string MapTypeHybrid = "G_HYBRID_MAP";
  34. //const string MapTypeTerrain = "G_PHYSICAL_MAP";
  35. public string Protocol
  36. {
  37. get { return protocol; }
  38. set { protocol = value; }
  39. }
  40. /// <summary>
  41. /// message to display if the api key is not found
  42. /// </summary>
  43. public string NoApiKeyWarning
  44. {
  45. get { return noApiKeyWarning; }
  46. set { noApiKeyWarning = value; }
  47. }
  48. /// <summary>
  49. /// default is 13, lower number zooms out higher number zooms in
  50. /// </summary>
  51. public int ZoomLevel
  52. {
  53. get { return zoomLevel; }
  54. set { zoomLevel = value; }
  55. }
  56. public MapType GmapType
  57. {
  58. get { return gmapType; }
  59. set { gmapType = value; }
  60. }
  61. /// <summary>
  62. /// This should be the path to the folder containing mojoPortal custom javascript for working with google APIs
  63. /// i.e ~/ClientScript/google/
  64. /// This is NOT the path to the google server.
  65. /// </summary>
  66. public string GoogleJsBasePath
  67. {
  68. get { return googleJsBasePath; }
  69. set { googleJsBasePath = value; }
  70. }
  71. /// <summary>
  72. /// To use google maps you need an API key.
  73. /// Get it here: http://code.google.com/apis/maps/signup.html
  74. /// put it in Web.config as <add key="GoogleMapsAPIKey" value="" />
  75. /// </summary>
  76. public string GMapApiKey
  77. {
  78. get { return gMapApiKey; }
  79. set { gMapApiKey = value; }
  80. }
  81. /// <summary>
  82. /// You can set this on the control if you need to use a different Web.config key for some reason.
  83. /// </summary>
  84. protected string GMapWebConfigKey
  85. {
  86. get { return gMapWebConfigKey; }
  87. set { gMapWebConfigKey = value; }
  88. }
  89. /// <summary>
  90. /// Indicates whether the GMapApiKey was initialized.
  91. /// Initialization happens in on pre-render
  92. /// </summary>
  93. protected bool ApiKeyFound
  94. {
  95. get { return apiKeyFound; }
  96. }
  97. /// <summary>
  98. /// Hooks up the main GMap script.
  99. /// SubClasses should override but call the base method before doing anything else.
  100. /// </summary>
  101. /// <param name="e"></param>
  102. protected override void OnPreRender(EventArgs e)
  103. {
  104. base.OnPreRender(e);
  105. LoadSettings();
  106. if (apiKeyFound)
  107. {
  108. IncludeGMapScript();
  109. }
  110. else
  111. {
  112. Literal litWarning = new Literal();
  113. litWarning.Text = noApiKeyWarning;
  114. this.Controls.Add(litWarning);
  115. }
  116. }
  117. private void LoadSettings()
  118. {
  119. if (gMapApiKey.Length > 0)
  120. {
  121. // don't look in Web.config if setting has been already made.
  122. apiKeyFound = true;
  123. return;
  124. }
  125. if (ConfigurationManager.AppSettings[gMapWebConfigKey] != null)
  126. {
  127. gMapApiKey = ConfigurationManager.AppSettings[gMapWebConfigKey];
  128. if (gMapApiKey.Length > 0) apiKeyFound = true;
  129. }
  130. }
  131. private void IncludeGMapScript()
  132. {
  133. Page.ClientScript.RegisterClientScriptBlock(
  134. typeof(Page),
  135. "gmap", "\n<script src=\""
  136. + protocol + "://maps.google.com/maps?file=api&amp;v=2&amp;key="
  137. + gMapApiKey
  138. + "\" type=\"text/javascript\"></script>");
  139. Page.ClientScript.RegisterClientScriptBlock(
  140. typeof(Page),
  141. "mojogmaputils", "\n<script src=\""
  142. + Page.ResolveUrl(googleJsBasePath + "mojogmaputils.js") + "\" type=\"text/javascript\"></script>");
  143. }
  144. }
  145. }