c# - 在谷歌地图上绘制多个标记

标签 c# javascript mysql google-maps

我正在从 MySQL 数据库中检索纬度和经度,并使用 Google map 使用标记显示检索到的坐标的位置,我正在尝试做这样的事情:

无效 Page_Load:

int RowLength;
String[] lon = null;
String[] lat = null;
protected void Page_Load(object sender, EventArgs e)
        {

            using (MySql.Data.MySqlClient.MySqlConnection connection = new MySql.Data.MySqlClient.MySqlConnection("Server;Port;Database=DB;Uid=U;Pwd=P"))
            {
                connection.Open();
               MySql.Data.MySqlClient.MySqlCommand cmd = connection.CreateCommand();


               cmd.CommandText = "SELECT longitude,latitude FROM table";

               MySql.Data.MySqlClient.MySqlDataReader datr = cmd.ExecuteReader();

               lon = new string[datr.FieldCount];
               lat = new string[datr.FieldCount];


               rep_loc = new string[datr.FieldCount];
               RowLength = datr.FieldCount;

                while (datr.Read())
                {
                        lon[counter] = datr[0].ToString();
                        lat[counter] = datr[1].ToString();
                        counter++;
                }
                datr.Close();
                connection.Close();
            }
}

Google map 的 Javascript

<script type="text/javascript">
    var lon;
    var lat;
    var homeLatlng;
    var mapOptions;
    var map;
    var contentString;
    var infowindow;

    function initialize() {

//I AM STUCK OVER HERE! and Don't know how to set multiple markers with information window for each marker
for(var i=0;i<='<%=RowLength%>';i++){
        homeLatlng = new google.maps.LatLng('<%=lat[0]%>', '<%=lon[0]%>');//initial

            mapOptions = {
                zoom: 5,
                center: homeLatlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };



        map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);


            marker = new google.maps.Marker({
                position: homeLatlng,
                map: map,
                url: "http://www.google.com",
            });

            contentString = '<div id="content" style="color:#000000;">' +
            'This is a test <br>' +
            '<a href="www.google.com"> google </a> <br>' +
            '</div>';



            infowindow = new google.maps.InfoWindow({
                content: contentString
            });

            google.maps.event.addListener(marker, 'click', function () { infowindow.open(map, marker) });
        }
    }
    google.maps.event.addDomListener(window, 'load', initialize);




</script>

有什么想法吗?

谢谢

最佳答案

我在 javascript 中使用以下代码完成了此操作:

<script type="text/javascript">

    var lat = <%= new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(lat)%>;
var lon = <%= new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(lon)%>;

function initialize() {

        mapOptions = {
            zoom: 4,
            center: new google.maps.LatLng(lat[0],lon[0]),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        infowindow = new google.maps.InfoWindow();


        map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);


        var len = '<%=RowLength%>';
        for (i = 0; i <= len ; i++) {

            marker = new google.maps.Marker({
                position: new google.maps.LatLng(lat[i], lon[i]),
                map: map,
                url: "http://www.google.com",
            });
google.maps.event.addListener(marker, 'click', (function (marker, i) {
                return function () {
                    infowindow.setContent("<div style='color:#000000';> Testing </div>");
                    infowindow.open(map, marker);
                }
            })(marker, i));


        }

    }

google.maps.event.addDomListener(window, 'load', initialize);


</script>

关于c# - 在谷歌地图上绘制多个标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23045278/

相关文章:

javascript - 是否可以延迟 window.load?

mysql - 在连接表中选择不同的值

php - PHP发布更新页面后需要刷新/重定向页面

mysql 过程 CONCAT 错误

c# - 使用和串流

c# - .Equals 和 == 有什么区别

javascript - 上层元素通过时如何保持底层元素悬停

c# - 将数据导出到 excel 时按钮可见错误的问题

c# - 为什么有时 2 个对象引用相同但不总是

javascript - 同时发送 XMLHttpRequest