I am having problems displaying a map. Data in ListView is showing on the page but the map is missing. I would appreciate some help. Here is what I have so far:
The following pieces of code are in MyDirectory.Master page:
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
function init_map(map_canvas_id, lat, lng, zoomLevel, markers) {
var myLatLng = new google.maps.LatLng(lat, lng);
var options = {
zoom: zoomLevel,
center: myLatLng,
mapTypeId: google.maps.MapTypeId.HYBRID
};
var map_canvas = document.getElementById(map_canvas_id);
var map = new google.maps.Map(map_canvas, options);
var bounds = new google.maps.LatLngBounds();
// Place markers
for (var i = 0; i < markers.length; i++) {
var marker = new google.maps.Marker(markers[i]);
marker.setMap(map);
bounds.extend(marker.getPosition());
}
// Center/zoom the map based on the bounds
map.fitBounds(bounds);
map.setCenter(bounds.getCenter());
}
And this piece is in MyDirectory.aspx page
<div id="my_map" style="width:500px;height:400px">
And this piece of code is in MyDirectory.aspx.cs page
protected void btnSearch_Click(object sender, EventArgs e)
{
var lat = 39;
var lng = -91;
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("Prefix", typeof(string)));
dt.Columns.Add(new DataColumn("First_Name", typeof(string)));
dt.Columns.Add(new DataColumn("Last_Name", typeof(string)));
dt.Columns.Add(new DataColumn("City", typeof(string)));
dt.Columns.Add(new DataColumn("State", typeof(string)));
dt.Columns.Add(new DataColumn("Zip_Code", typeof(string)));
List<string> markers = new List<string>();
List<DataDetail> dataStudents = (new BLL.MyDirectory.BLLDirectory()).GetAndBindInfo(txtSearchByLastName.Text);
foreach (DataDetail item in dataStudents)
{
dr = dt.NewRow();
dr["Prefix"] = item.Prefix;
dr["First_Name"] = item.First_Name;
dr["Last_Name"] = item.Last_Name;
dr["City"] = item.City;
dr["State"] = item.State;
dr["Zip_Code"] = item.Zip_Code;
dt.Rows.Add(dr);
markers.Add(" { title : \"" + item.Last_Name + "\", position: new google.maps.LatLng(" + item.Latitude + ", " + item.Longitude + " } ");
}
dynamic locations = "[" + string.Join(",", markers.ToArray()) + "]";
ClientScript.RegisterStartupScript(this.GetType(), "LoadMap", string.Format("init_map('my_map', {0}, {1}, 13, {2});", lat, lng, locations), true);
DataTable myDt = new DataTable();
myDt = dt;
Session["myDataTable"] = myDt;
lvSearchResults.DataSource = ((DataTable)Session["myDataTable"]).DefaultView;
lvSearchResults.DataBind();
}