FineUI与百度地图简单示例 (转帖)
http://www.fineui.com/bbs/forum.php?mod=viewthread&tid=4191&extra=page%3D1
前台代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Map.aspx.cs" Inherits="EmptyProjectNet40.Map" %>
<!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> <style type="text/css"> .columnpanel { margin-right: 5px; } .rowpanel { margin-bottom: 5px; } </style> <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.5&ak=C2c1257ee55b0cdb7d7df0ae4c3f4521"></script> </head> <body> <form id="form1" runat="server"> <x:PageManager ID="PageManager1" runat="server" /> <x:Panel ID="Panel2" runat="server" Height="500px" Width="900px" ShowBorder="True" BodyPadding="5px" Layout="Column" ShowHeader="True" Title="百度地图示例"> <Items> <x:Panel ID="Panel4" ColumnWidth="75%" EnableBackgroundColor="true" runat="server" CssClass="columnpanel" BodyPadding="5px" ShowBorder="true" ShowHeader="true" Layout="Fit" Height="464px" Title="地图"> <Toolbars> <x:Toolbar ID="Toolbar1" runat="server" Position="Top"> <Items> <x:Button runat="server" ID="Button1" Text="移动当前城市" EnablePostBack="false" OnClientClick="panTo();"> </x:Button> </Items> </x:Toolbar> </Toolbars> <Items> <x:ContentPanel runat="server" ShowBorder="True" ShowHeader="false"> <div id="container1" style="width:100%; height:400px; "></div> </x:ContentPanel> </Items> </x:Panel> <x:Panel ID="Panel3" ColumnWidth="25%" EnableBackgroundColor="true" runat="server" BodyPadding="5px" ShowBorder="true" ShowHeader="true" Height="464px" Title="坐标"> <Items> <x:Grid ID="Grid1" Title="表格" PageSize="4" ShowBorder="true" ShowHeader="False" runat="server" Height="427px" IsDatabasePaging="true" EnableRowDoubleClickEvent="true" OnRowDoubleClick="Grid1_RowClick"> <Columns> <x:BoundField Width="55px" DataField="CityName" DataFormatString="{0}" HeaderText="地区" /> <x:BoundField Width="75px" DataField="pointX" DataFormatString="{0}" HeaderText="经度" /> <x:BoundField Width="75px" DataField="pointY" DataFormatString="{0}" HeaderText="纬度" /> </Columns> </x:Grid> </Items> </x:Panel> </Items> </x:Panel> </form> <script type="text/javascript"> var map; var gridClientID = '<%= Grid1.ClientID %>'; var gc; function onReady() { map = new BMap.Map("container1"); // 创建地图实例 var point = new BMap.Point(116.404, 39.915); // 创建点坐标 map.centerAndZoom(point, 15); map.addEventListener("click", showInfo); //百度API注册单击事件 gc = new BMap.Geocoder(); } //移动到当前城市 function panTo() { var myCity = new BMap.LocalCity(); myCity.get(myFun); } //移动到某个经度和纬度 function PanToCity(x,y) { map.panTo(new BMap.Point(x, y)); } function myFun(result) { var cityName = result.name; map.setCenter(cityName); } //百度API点击回调事件 function showInfo(e) { //可以把获取到的经度和纬度保存起来,或者动态添加到Grid中 var pt = e.point; gc.getLocation(pt, function (rs) { var addComp = rs.addressComponents; __doPostBack(addComp.city + "," + e.point.lng + "," + e.point.lat, 'specialkey');//获取坐标简单拼装发送给服务端 });
} </script> </body> </html>
后台代码
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using Newtonsoft.Json.Linq; using FineUI;
namespace EmptyProjectNet40 { public partial class Map : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GetData(); } else { if ( Request.Form["__EVENTARGUMENT"] == "specialkey") { string req = Request.Form["__EVENTTARGET"]; string[] point=req.Split(','); if (point.Length > 1) { JObject defaultObj = new JObject(); defaultObj.Add("CityName", point[0]); defaultObj.Add("pointX", point[1]); defaultObj.Add("pointY", point[2]); DataTable dbt = (DataTable)Session["KEY_FOR_DATASOURCE_SESSION"]; dbt.Rows.Add(new object []{point[0],point[1],point[2]}); Session["KEY_FOR_DATASOURCE_SESSION"] = dbt; Grid1.DataSource = dbt; Grid1.DataBind(); } } } } protected void Grid1_RowClick(object sender, FineUI.GridRowClickEventArgs e) { DataTable dbt = (DataTable)Session["KEY_FOR_DATASOURCE_SESSION"]; DataRow dr = dbt.Rows[e.RowIndex]; string x = Convert.ToString(dr["pointX"]); string y = Convert.ToString(dr["pointY"]); PageContext.RegisterStartupScript("PanToCity(" + x + "," + y + ");"); } private DataTable dt; public void GetData() { dt = new DataTable(); dt.Columns.Add("CityName"); dt.Columns.Add("pointX"); dt.Columns.Add("pointY"); dt.Rows.Add(new object[] { "北京市", "116.222", "39.2222" }); dt.Rows.Add(new object[] { "武汉市", "114.333", "30.222"}); Grid1.DataSource = dt; Grid1.DataBind(); Session["KEY_FOR_DATASOURCE_SESSION"] = dt; } } }