【问题标题】:Generating Google Maps markers From Database从数据库生成谷歌地图标记
【发布时间】:2015-11-03 01:48:23
【问题描述】:

您好,我正在尝试使用 SQL Server 数据库表中的多个标记填充 Google Maps V3。

我将纬度经度保存在名为 Location 的一列中,格式为 38.425424,-5.477177

我的问题是标记没有出现在地图中

script type="text/javascript">
     var markers = [
    <asp:Repeater ID="rptMarkers" runat="server">
    <ItemTemplate>
                {
                    "title": '<%# Eval("Cust_Name") %>',
                    "Location": '<%# Eval("Location") %>',
                "description": '<%# Eval("description") %>'
            }
</ItemTemplate>
<SeparatorTemplate>
    ,
</SeparatorTemplate>
</asp:Repeater>
 ];
</script>
<script type="text/javascript">
    window.onload = function () {
        var mapOptions = {
            center: new google.maps.LatLng(markers[0].Location),
            zoom: 8,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var infoWindow = new google.maps.InfoWindow();
        var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
        for (i = 0; i < markers.length; i++) {
            var data = markers[i]
            var myLatlng = new google.maps.LatLng(data.Location);
            var marker = new google.maps.Marker({
                position: myLatlng,
                map: map,
                title: data.title
            });
            (function (marker, data) {
                google.maps.event.addListener(marker, "click", function (e) {
                    infoWindow.setContent(data.description);
                    infoWindow.open(map, marker);
                });
            })(marker, data);
        }
    }
</script>
<div class="panel-body" id="dvMap" style="height:280px;" >                                                            
                                    
</div>
protected void Page_Load(object sender, EventArgs e)
    {
                if (!this.IsPostBack)
        {
            DataTable dt = this.GetData("select * from Customer");
            rptMarkers.DataSource = dt;
            rptMarkers.DataBind();
        }

    }

    private DataTable GetData(string query)
    {
        string conString = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
        SqlCommand cmd = new SqlCommand(query);
        using (SqlConnection con = new SqlConnection(conString))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;

                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    return dt;
                }
            }
        }
    }

【问题讨论】:

  • 你的 JavaScript 在客户端是什么样子的?您尝试使用中继器创建的数组中是否有任何数据?
  • rptMarkers.DataSource = dt; rptMarkers.DataBind();
  • 是的,这就是在服务器端发生的事情,但是当您在浏览器中时,页面呈现后您的脚本会是什么样子?标记数组中是否有任何项目?
  • 是的,数组中有项目
  • 哦,看起来您将标记的纬度和经度呈现为单个字符串。 google.maps.LatLng() 构造函数接受两个参数,它可能会看到单个字符串并引发运行时错误。当您的页面运行时,您在控制台中遇到了哪些错误?

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


【解决方案1】:

您将标记的纬度和经度呈现为单个字符串。 google.maps.LatLng() 构造函数有两个参数,一个是纬度,一个是经度。它看到您存储 lat 和 long 的单个字符串值,并引发运行时错误,因为构造函数要求它们是两个单独的参数。

不要将 lat 和 long 作为一个字段存储在数据库中,而是将它们分成两个字段,然后将它们拆分为数组中的两个索引,然后将它们分别传递给 google.maps.LatLng() 的相应参数构造函数。

或者,如果您无法更改您的数据库架构,split the single string into two values 在将其传递给构造函数之前。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 2011-02-28
    • 2016-11-27
    • 1970-01-01
    • 1970-01-01
    • 2012-06-06
    相关资源
    最近更新 更多