显示随机选择的地图标记?

时间:2013-10-04 09:05:35

标签: c# javascript asp.net google-maps google-maps-api-3

当我从另一个从地址生成latlng的页面获取latlng时,可以显示标记。但是当我从随机拾取生成latlng的页面中获取latlng时,所有标记都没有显示出来。这是正常的还是我自己的错误?先感谢您。

我将latlng存储在sqlserver中。所以,这些是代码。

这些是后端代码。

 DataTable dt = new DataTable();
        using (SqlConnection con = new SqlConnection(connstr))
        {
            using (SqlCommand cmd = new SqlCommand("select title=city,lat=latitude,lng=longitude,description from googleMap", con))
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
                Dictionary<string, object> row;
                foreach (DataRow dr in dt.Rows)
                {
                    row = new Dictionary<string, object>();
                    foreach (DataColumn col in dt.Columns)
                    {
                        row.Add(col.ColumnName, dr[col]);
                    }
                    rows.Add(row);
                }
                return serializer.Serialize(rows);

这些是javascript。

 function initialize() {
    var markers = JSON.parse('<%=ConvertDataTabletoString() %>');
    var mapOptions = {
        center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
        zoom: 4,
        mapTypeId: google.maps.MapTypeId.ROADMAP
        //  marker:true
    };
    var infoWindow = new google.maps.InfoWindow();
    var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
    for (i = 0; i < markers.length; i++) {
        var data = markers[i]
        var myLatlng = new google.maps.LatLng(data.lat, data.lng);
        var marker = new google.maps.Marker({
            position: myLatlng,
            map: map,
            title: data.title
        });
        (function (marker, data) {

            // Attaching a click event to the current marker
            google.maps.event.addListener(marker, "click", function (e) {
                infoWindow.setContent(data.description);
                infoWindow.open(map, marker);
            });
        })(marker, data);
    }
}
google.maps.event.addDomListener(window, 'load', initialize);

0 个答案:

没有答案