基于ASP.NET技术将数据库中坐标点数据呈现到GOOGLE地图上 (google map开发高级篇)

本文主要记录了将数据库中坐标点数据呈现到GOOGLE地图上的相关代码,本文主要用到的技术有asp.net、jquery、google map api等。废话不多说代码如下:

1、  页面代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GoogleMap.aspx.cs" Inherits="LdConsole.Web.GoogleMap" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

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

<head runat="server">

    <title></title>

    <script src="http://ditu.google.cn/maps?file=api&amp;v=2.x&amp;key=ABQIAAAA48kTsSent7VKDiynLll5-BSenMbw3-kZLokidiRzbXMRfaqj2RTxeyIWvnSmTZOhXWzCeitYb-2Bng"

        type="text/javascript"></script>

    <script src="../Js/lib/jquery-1.5.1.min.js" type="text/javascript"></script>

    <script type="text/javascript">

        var CollectorName = "";

        function adjustLight() {

            var text = document.getElementById('test').value;

            $("#message").html("调整亮度成功!"+text);

        }

 

        function initialize() {

            //创建地图对象

            if (GBrowserIsCompatible()) {

                var map = new GMap2(document.getElementById("map_canvas"));

                map.setCenter(new GLatLng(39.917, 116.397), 14);

                //jquery读取数据库灯杆信息,并创建地图标记

                function createMarker(latlng, number) {

                    var marker = new GMarker(latlng);

                    marker.value = number;

                    GEvent.addListener(marker, "click", function () {

                        //在标签里面生成测量点信息(灯泡)

                        var myHtml = "亮度值:<input id=\"test\"> <input type=\"button\"  onClick=\"adjustLight()\" value=\"调整亮度\">";

                        map.openInfoWindowHtml(latlng, myHtml);

                    });

                    return marker;

                }

                //生成灯杆地图标记

                var queryString = "1=1";

                var tmp = "";

                $.ajax({

                    type: "get",

                    dataType: "json",

                    url: 'ServerAjax.ashx?method=GetMapFOI&queryString=' + queryString,

                    data: "tmp=" + tmp,

                    complete: function () { $("#load").hide(); }, //

                    success: function (msg) {//

                        var data = msg.table;

                        $.each(data, function (i, n) {

                            var latlng = new GLatLng(n.Longitude, n.Latitude);

                            map.addOverlay(createMarker(latlng, i + 1));

                        });

                    }

                });

                //

            }

        }

        

    </script>

</head>

<body οnlοad="initialize()" οnunlοad="GUnload()">

    <div id="map_canvas" style="width: 500px; height: 300px">

    </div>

    <div id="message">

    </div>

</body>

</html>

2、  后台代码:

新建ServerAjax.ashx文件,代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Data;

using System.Text;

using System.Data.SqlClient;

using System.Configuration;

using System.Web.UI;

 

namespace LdConsole.Web

{

    /// <summary>

    /// ServerAjax 的摘要说明

    /// </summary>

    public class ServerAjax : IHttpHandler

    {

 

        public void ProcessRequest(HttpContext context)

        {

            string method = System.Web.HttpContext.Current.Request.QueryString["method"].Trim();

            string queryString = System.Web.HttpContext.Current.Request.QueryString["queryString"].Trim();

           

            switch (method)

            {

                case "GetMapFOI"://生成地图标记

                    context.Response.Write(GetMapFOI(queryString));

                    break;

            }

        }

        //根据灯杆库里的数据生成地图上灯杆标记 QueryString为查询筛选条件

        private String GetMapFOI(string queryString)

        {

            string sql;

            sql = "select * from tbCollector where " + queryString;

            string dbfile = ConfigurationManager.ConnectionStrings["KwstuConn"].ToString();

            SqlConnection conn = new SqlConnection(dbfile);

            SqlDataAdapter da = new SqlDataAdapter(sql, conn);

 

            DataTable dt = new DataTable("table");

            da.Fill(dt);

            return DataTable2Json(dt);

        }

        //生成Json数据格式方法

        private string DataTable2Json(DataTable dt)

        {

            StringBuilder jsonBuilder = new StringBuilder();

            jsonBuilder.Append("{\"");

            jsonBuilder.Append(dt.TableName);

            jsonBuilder.Append("\":[");

            for (int i = 0; i < dt.Rows.Count; i++)

            {

                jsonBuilder.Append("{");

                for (int j = 0; j < dt.Columns.Count; j++)

                {

                    jsonBuilder.Append("\"");

                    jsonBuilder.Append(dt.Columns[j].ColumnName);

                    jsonBuilder.Append("\":\"");

                    jsonBuilder.Append(this.JsonStringFormat(dt.Rows[i][j].ToString()));

                    jsonBuilder.Append("\",");

                }

                jsonBuilder.Remove(jsonBuilder.Length - 1, 1);

                jsonBuilder.Append("},");

            }

            jsonBuilder.Remove(jsonBuilder.Length - 1, 1);

            jsonBuilder.Append("]");

            jsonBuilder.Append("}");

            return jsonBuilder.ToString();

        }

 

        //Json数据源字符串格式化方法 防止返回的Json数据中有非法字符造成程序出错

        public string JsonStringFormat(string JsonString)

        {

            return JsonString.Trim().Replace("\"", "\\\"").Replace("\\", "/").Replace("\r\n", "").Replace("\r", "").Replace("\r\r", "").Replace("\n", "").Replace("\n\r", "");

        }

        public bool IsReusable

        {

            get

            {

                return false;

            }

        }

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值