Handling Google Maps API Keys with ASP.NETand Web.config

Sign up for the Google Maps API for every URL the site will exist on. Add the keys to the appSettings section of Web.config.

 <appSettings>
   <!-- Google Map API Keys -->
   <add key="localhost" value="http://maps.google.com/maps?file=api&v=2&key="/>

 </appSettings>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GoogleMap.aspx.cs" Inherits="GoogleMap" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <meta http-equiv="content-type" content="text/html; charset=utf-8" />
   <title>Google Maps JavaScript API Example</title>

   <script type="text/javascript">

       //<![CDATA[

       function load() {
           if (GBrowserIsCompatible()) {
               var map = new GMap2(document.getElementById("map"));
               map.setCenter(new GLatLng(37.4419, -122.1419), 13);
           }
       }

       //]]>
   </script>

</head>
<body onload="load()" onunload="GUnload()">
   <form id="Form1" runat="server">
   <div id="map" style="width: 500px; height: 300px">
   </div>
   </form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class GoogleMap : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {



  }
  protected void Page_Init(object sender, EventArgs e)
  {
      if (!Page.IsStartupScriptRegistered("googleMapScript"))
      {
          // Register Startup Script for Google Maps API
          string myScript = "&lt;script type=\"text/javascript\" src=\""
+ ConfigurationManager.AppSettings["localhost"] +"\"&gt;&lt;/script&gt;";
          this.Page.ClientScript.RegisterStartupScript(typeof(Page), "googleMapScript", myScript);
      }
  }
}
Reactions

Post a Comment

0 Comments

Close Menu