在页面上显示谷歌地图时出现问题

时间:2013-12-07 19:38:42

标签: c#

我在显示地图时遇到问题。 ListView中的数据显示在页面上,但缺少地图。我将不胜感激。以下是我到目前为止的情况:

以下代码片段位于MyDirectory.Master页面中:

<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());
}

这篇文章在MyDirectory.aspx页面

<div id="my_map" style="width:500px;height:400px">

这段代码在MyDirectory.aspx.cs页面

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();               
    }

0 个答案:

没有答案