如何将经纬度利用Google Map API显示C# VS2005 Sample Code

原文 如何将经纬度利用Google Map API显示C# VS2005 Sample Code

日前写了一篇如何用GPS抓取目前所在,并回传至资料库储存,这篇将会利用这些回报的资料,将它显示在地图上,这个做法有两种,最简单的就是直接传值到Google Maps上.

 

举例来说,当我们知道经纬度后,只要将数据套到以下网址即可.

http://maps.google.com/maps?q=25.048346%2c121.516396

在参数q=后面,就可以加上经纬度了.

25.048346是Latitude纬度

%2c是空格

121.516396就是Longitude经度了.

范例画面:

image

 

而另一种做法就比这个复杂一点,要使用Google API来做,首先,要使用google API就必需要有google的帐号,没帐号是无法申请的,当有google的帐号后,就可以到http: //code.google.com/apis/maps/signup.html开始申请了.

image

最 下方My web site URL就输入各位的URL啰,如果输入的与执行google map api的URL不同,那就无法执行了.所以这个URL务必输入正确, 输入正确的URL并将上方的CheckBox打勾后,就可以按Generate API Key了,如果已经登入GOOGLE的,就不会再跳登入画面,之后就会跳到另一个画面,上面就有Key及Example Code了,当有了这些,就可以开始自己写Code了.

 

基本上,因为主要是Demo用的,所以设计介面很简单.

map1

上面就一个DropDownList,因为先前的范例资料的关系,先手动在ITEM上加上1跟2.

而下方的地图,就跟申请API时的Example Code一样. 原始码如下:

 

 <%@ Page Language= "C#" AutoEventWireup= "true" CodeFile= "Default.aspx.cs" Inherits= "_Default" %>  <!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>GPS 位置地图</title>  <script src= "http://maps.google.com/maps?file=api&amp;v=2&amp;key=输入你的Key"  type= "text/javascript" ></script>  <script src= "http://www.google.com/uds/api?file=uds.js&v=1.0&key=输入你的Key"  type= "text/javascript" ></script>  <script src= "http://www.google.com/uds/solutions/localsearch/gmlocalsearch.js" type= "text/javascript" ></script>  <style type= "text/css" > @import url( "http://www.google.com/uds/css/gsearch.css" );  @import url( "http://www.google.com/uds/solutions/localsearch/gmlocalsearch.css" );  </style>  <script type= "text/javascript" >  //<![CDATA[  function load(x,y,LocationName) {  if (GBrowserIsCompatible()) {  var map = new GMap2(document.getElementById( "map" ));  var point = new GLatLng(x,y);  map.setCenter(point, 16);  map.addOverlay( new GMarker(point));  map.addControl( new GLargeMapControl());  map.addControl( new GMapTypeControl());  map.addControl( new GScaleControl());  var lsc = new google.maps.LocalSearch();  map.addControl( new google.maps.LocalSearch() , new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,20)));  map.openInfoWindowHtml (map.getCenter(), LocationName);  }  }  //]]>  </script>  </head>  <body id= "MainBody" runat= "server" >  <form id= "form1" runat= "server" >  <div>  <asp:DropDownList ID= "ddl_Location" runat= "server" AutoPostBack= "True" OnSelectedIndexChanged= "ddl_Location_SelectedIndexChanged"  Width= "500px" >  <asp:ListItem>1</asp:ListItem>  <asp:ListItem Value= "2" >2</asp:ListItem>  </asp:DropDownList><br />  <br />  <div id= "map" style= "width: 500px; height: 400px" >  </div>  </div>  </form>  </body>  </html> 

 

 

 

只要将"输入你的Key"的地方置换为你在Google MAP API申请到的Key即可.

 

    protected void Page_Load( object sender, EventArgs e)
     {
         if (!IsPostBack)  {  if (Request.QueryString.HasKeys())  {  string longitude = Request.QueryString.Get( "lon" );  string latitude = Request.QueryString.Get( "lat" );  string LN = Request.QueryString.Get(Server.UrlDecode( "LN" ));  this .MainBody.Attributes.Add( "onload" , "load(" + longitude + "," + latitude + ",'" + LN + "')" );  }  else  {  DataTable dt = GetLocation(ddl_Location.SelectedValue);  if (dt.Rows.Count > 0)  {  DataRow dr = dt.Rows[0];  this .MainBody.Attributes.Add( "onload" , "load(" + dr[ "Latitude" ].ToString() + "," + dr[ "Longitude" ].ToString() + ",'" + dr[ "updtime" ].ToString() + "')" );  }  }  }  }  protected void ddl_Location_SelectedIndexChanged( object sender, EventArgs e)  {  try  {  DataTable dt = GetLocation(ddl_Location.SelectedValue);  if (dt.Rows.Count > 0)  {  DataRow dr = dt.Rows[0];  this .MainBody.Attributes.Add( "onload" , "load(" + dr[ "Latitude" ].ToString() + "," + dr[ "Longitude" ].ToString() + ",'" + dr[ "updtime" ].ToString() + "')" );  }  }  catch (Exception ex)  {  Response.Write(ex.Message);  }  }  private DataTable GetLocation( string UID)  {  try  {  string strconn = "Data Source=localhost;Initial Catalog=GPSDB;User Id=GPSUser;Password=gpsuser;" ;  SqlConnection conn = new SqlConnection(strconn);  string strcmd = "select Latitude,Longitude,UpdTime from GPSDB..gpstrace where UID=@UID" ;  SqlCommand cmd = new SqlCommand(strcmd, conn);  cmd.Parameters.AddWithValue( "@UID" , UID);  SqlDataAdapter da = new SqlDataAdapter(cmd);  DataTable dt= new DataTable();  da.Fill(dt);  return dt;  }  catch (Exception ex)  {  throw new Exception(ex.Message);  }  } 

 

 

 

如此一来,就大功告成了,但或许有些人会有些遗问,那么MAP上,可以自订一些东西,例如不给搜寻列,这都是可以做到的,可以参考Google Map API Examples ,这里就有很多详细的说明.

 

感觉起来,GPS定位的想法部份,好像到此就没了,但在这过程中也发现到,其实Google Map有出Mobile版 的,而它的定位可不只是局限在GPS卫星讯号,而是可以用手机的讯号去定位,也就是说,他是透过手机与基地台之间的传输来计算出所在位置,这样就可以不受 到手机没有GPS功能模组或收不到卫星讯号所限制,这个概念其实也不算新,记得几年前的Run!PC杂志上就有篇文章是在介绍这个的,采用的技术是 Java.

 

不过不管如何,可以预见的是,这个的应用会愈来愈多元,谁说未来还要自己去用电脑下载图资再更新到自己的GPS装置上,装置上的地图永远会是最新的,加上Street View,也不用去看那电脑画出来的3D的道路图了,或许3G或无线上网的普及,这些运用将会更广泛.

 

参考资料:

Google Map API Examples

Google Map Mobile

 

原始码: GPSMap.zip

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值