簡體   English   中英

如何使用來自mssql datebase的數據單擊C#.net頁面中包含的JavaScript塊內的每個標記來添加infowindow

[英]How to add infowindow by clicking on each marker inside JavaScript Block contained within a C# .net Page using data from mssql datebase

我從mssql datebase獲取經度和經度,一切正常,但我想通過使用來自同一日期庫的信息點擊每個標記添加infowindow,我不知道我該怎么做。

我的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>

任何答案/建議都是值得贊賞的。謝謝......

你有這條線的地方:

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

但問題是,它為您設置的最后一個Marker創建一個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);
            });

我希望這可以幫助你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM