How to use latitude and longitude to display C # VS2005 Sample Code using the Google Map API

Source: Internet
Author: User

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

范例画面:

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

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

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

上面就一个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" >"Http://www.w3.org/1999/xhtml" >"Server" ><title>gps Location Map </title><script src="Http://maps.google.com/maps?file=api&amp;v=2&amp;key= Enter your Key"Type="Text/javascript" ></script><script src="Http://www.google.com/uds/api?file=uds.js&v=1.0&key= Enter your 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><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= span class= "str" > "500px" >  <asp:ListItem>1</asp:ListItem>  <asp:listitem value= " 2 ">2</asp:ListItem>  </asp:dropdownlist><br/>  <br/>  <div id= span class= "str" > "map" style=  </div>  </div>  </form> </body>  

只要将"输入你的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 [email protected] ";  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);  }          /span>           

如此一来,就大功告成了,但或许有些人会有些遗问,那么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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.