3

我从 mssql 数据库中获取我的纬度和经度,一切正常,但我想通过使用来自同一数据库的信息单击每个标记来添加信息窗口,我不知道该怎么做。

我的 WebForm1.aspx.cs 页面:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data.SqlTypes;
using System.Data;
using System.Data.OleDb;
using System.Data.Common;
using System.Windows.Forms;
using AjaxControlTolkit;

namespace WebApplication3

{

public partial class WebForm1 : System.Web.UI.Page

{

    SqlConnection sqlConn;
    SqlCommand sqlCmd;
    SqlDataAdapter sqlDatatAdapter;
    DataSet ds_pc;

 protected void Page_Load(object sender, EventArgs e)
 {
    ds_pc = new DataSet();
    sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
    sqlCmd = new SqlCommand();
    sqlDatatAdapter = new SqlDataAdapter();
    sqlCmd = new SqlCommand("dbo.PROCEDURE", sqlConn);
    sqlCmd.CommandType = CommandType.StoredProcedure;
    sqlDatatAdapter = new SqlDataAdapter(sqlCmd);
    sqlDatatAdapter.Fill(ds_pc);

    string Locations = "" ;
    string txt_site ; 
    string lat ;
    string lon ;

    foreach(DataRow dr in (ds_pc.Tables[0]).Rows)
    {
         if (dr["LATITUDE"].ToString().Trim().Length == 0)
         continue;

         txt_site = dr["SITE"].ToString();
         lat = dr["LATITUDE"].ToString();
         lon = dr["LONGITUDE"].ToString();

         Locations += Environment.NewLine + " map.addOverlay(new GMarker(new GLatLng(" + lat + "," + lon + ")));";
         }

         js.Text = @"<script type='text/javascript'>
         function initialize() {
               if (GBrowserIsCompatible()) {
               var map = new GMap2(document.getElementById('map_canvas'));
               map.setCenter(new GLatLng(42.00, 43.30), 8);"+Locations+@"map.setUIToDefault();

              }
             }
       </script> ";
            }
        }

我的 WebForm1.aspx 页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication3.WebForm1" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">
<title>Google Maps</title>

<script type="text/javascript" src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=MyKeyA&sensor=false"></script>

<body onload="initialize()" onunload="GUnload()">
<div>
<asp:Panel id="Panel1" runat="server">
<asp:Literal id="js" runat="server"></asp:Literal>
<div id="map_canvas" style="width: 1000px; height: 615px"></div>
</asp:Panel>
</div>
</body>
</html>

任何答案/建议都是可观的..谢谢..

4

1 回答 1

1

你有这条线的地方:

    Locations += Environment.NewLine + " map.addOverlay(new GMarker(new GLatLng(" + lat + "," + lon + ")));";

您需要添加一些类似的内容:

    Locations += Environment.NewLine + " map.addOverlay(new google.maps.event.addListener(GMarker, 'click', function () {
            new google.maps.InfoWindow({
                content: "String for content"
            });
        });";

但是,问题在于它会为您设置的最后一个标记创建一个 InfoWindow,因为事件会动态创建 InfoWindow。

一个好的解决方案是将您的标记存储在一个数组中,然后在 javascript 中您可以拥有

    markerArray[index]['window'] = new google.maps.InfoWindow({
                content: "String for content"
            });

这也将允许您为每个只需要调用的标记附加一个单击侦听器事件

    new google.maps.event.addListener(markerArray[index], 'click', function() {
                this['window'].open(map, this);
            });

我希望这可以帮助你

于 2013-03-15T09:19:26.527 回答