I know there are quite a few questions related to this, but mine is quite specific. I'm using an example from an online article. Here is my code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="GoogleMapsAPItesting.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Google Maps Markers</title>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=InsertAPIKeyhere&sensor=false">
</script>
<script type="text/javascript">
function initialize() {
}
</script>
</head>
<body onload="initialize()">
<form id="WebForm1" runat="server">
<div id="mapArea" style="width: 500px; height: 500px;">
</div>
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
</form>
</body>
</html>
And the Code Behind C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
namespace GoogleMapsAPItesting
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string markers = GetMarkers();
Literal1.Text = @"
<script type=’text/javascript’>
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(28.3213, 77.5435),
zoom: 2,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var myMap = new google.maps.Map(document.getElementById(‘mapArea’),
mapOptions);" + markers + @"}
</script>";
}
protected string GetMarkers()
{
string markers = "";
SqlConnection conn = new SqlConnection();
conn.ConnectionString =
"Data Source=FakeServer;" +
"Initial Catalog=FakeInitCatalog;" +
"User id=FakeUID;" +
"Password=FakePass;";
{
SqlCommand cmd = new SqlCommand("SELECT Latitude, Longitude, City FROM Locations", conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
int i = 0;
while (reader.Read())
{
i++;
markers +=
@"var marker" + i.ToString() + @" = new google.maps.Marker({
position: new google.maps.LatLng(" + reader["Latitude"].ToString() + ", " +
reader["Longitude"].ToString() + ")," +
@"map: myMap,
title:’" + reader["City"].ToString() + "’});";
}
}
return markers;
}
}
}
When I run this, the map is not displayed, just a blank page. If I take the C# code for literal1, and place it in the HTML/Asp page, the map loads (but the markers are not placed). I feel like I'm just missing one piece. I really appreciate any suggestions.
Thank you.