码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2014-06-27 22:50:28      阅读:544      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   java   get   使用   

原文 如何将经纬度利用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经度了.

范例画面:

bubuko.com,布布扣

 

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

bubuko.com,布布扣

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

 

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

bubuko.com,布布扣

上面就一个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

 

如何将经纬度利用Google Map API显示C# VS2005 Sample Code,布布扣,bubuko.com

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

标签:style   blog   http   java   get   使用   

原文地址:http://www.cnblogs.com/lonelyxmas/p/3811122.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!