開發(fā)平臺:
操作系統(tǒng): Window XP
開發(fā)環(huán)境:
Visual Studio 2005
Windows Mobile 5.0 Pocket PC SDK
.Net Compact Framework 2.0 (VS2005自帶)
ActiveSync 4.0
移動設(shè)備:
Dell X51 PDA + GPS 卡
1. 環(huán)境的搭建
1) 安裝Visual Stuido 2005
2) 安裝ActiveSync4.0(或更新版本)
3) 安裝Windows Mobile 5.0 Pocket PC SDK(VS2005默認安裝WM2003SDK,所以需要手動安裝WM5的SDK)
以上所需在網(wǎng)上均可找到下載,安裝過程應該比較簡單,沒有什么復雜的設(shè)置所以略過不談.有不明白的可以發(fā)E-Mail咨詢.
2. 詳細步驟
1) 啟動VS2005.第一次啟動會提示設(shè)置默認開發(fā)模式,可以選擇Visual C#.
2) 點擊 [文件]->[新建項目]
如圖:
a) 項目類型:選擇Visual C#à智能設(shè)備àWindows Mobile 5.0 Pocket PC(如果沒有該選項則說明沒有安裝WM5.0SDK或者安裝失敗)
b) 模板:選擇設(shè)備應用程序即可
c) 輸入名稱,位置,解決方案名稱等信息后點擊確定即可.
1) 點擊[文件]à添加à現(xiàn)有項目
找到..\Program Files\Windows CE Tools\wce500\Windows Mobile 5.0 Pocket PC SDK\Samples\Cs\Gps即可找到Microsoft.WindowMobile.Samples.Location.csproj項目文件,該項目封裝了訪問GPS硬件的一些API函數(shù).使用非常方便.沒有找到該文件的話請確認是否安裝了WM5.0SDK.
打開后即可添加到現(xiàn)有項目中.如下圖示:
1) 設(shè)置項目依賴性
點擊[項目]->項目依賴性
因為要在TestGPS項目中引用添加的項目,所以TestGPS項目依賴于Microsoft.WindowsMobile.Samples.Location
生成順序自然就是TestGPS在后了.
1) 添加項目引用
點擊à[項目]à添加引用
選擇[項目]頁,選擇當前項目后確定.即可在TestGPS項目的引用列表中看到對該項目的引用.
添加對項目類包的引用.
6)說明
在引入了類包之后,我們就可以在程序中引用已經(jīng)封裝好的類來訪問GPS了.在項目中我們可以看到常用的幾個類:
DegreesMinutesSeconds.cs //主要負責經(jīng)緯度坐標度分秒的轉(zhuǎn)換
DeviceStateChangedEventArgs.cs //GPS設(shè)備狀態(tài)改變時觸發(fā)的事件
GPS.cs //操作GPS的類,主要有負責Open()和Close()GPS設(shè)備.
GpsDeviceState.cs //GPS設(shè)備的幾種狀態(tài)
GpsPosition.cs //處理經(jīng)緯度坐標的類.
LocationChangedEventArgs.cs //位置改變時觸發(fā)的事件(即經(jīng)緯度坐標發(fā)生變化)
需要說明的是,我在使用GpsPosition類的Longitude和Latitude屬性獲取經(jīng)緯度坐標的時候總是出現(xiàn)DividedByZeroException的例外.經(jīng)過觀察發(fā)現(xiàn)是由于對度分秒格式的經(jīng)緯度坐標值轉(zhuǎn)化為Decimal Degrees表達形式的時候出錯(看了代碼之后大家理解的會比我說的更明白,所以看不明白這一點的不必介意因為我的表述也不是很清楚!),而我需要的其實就是最原始的double類型的經(jīng)緯度坐標值,不需要進行任何轉(zhuǎn)換即可.所以我對GpsPosition類進行了簡單的修改以滿足我的需要.
在GpsPositon類的末尾加入一下幾行代碼.
public double DoubleLatitude
以上提到這些只是為可能會和我有同樣需求的初學的網(wǎng)友提個醒,免得走彎路.
1. 附參考源代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsMobile.Samples.Location;
namespace TestGPS
{
public partial class Form1 : Form
{
//GPS設(shè)備狀態(tài)對象
GpsDeviceState device = null;
//GPS位置對象
GpsPosition position = null;
//GPS對象
Gps gps = new Gps();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
gps.DeviceStateChanged += new DeviceStateChangedEventHandler(gps_DeviceStateChanged);
gps.LocationChanged += new LocationChangedEventHandler(gps_LocationChanged);
}
//位置改變時更新坐標數(shù)據(jù)
protected void gps_LocationChanged(object sender, LocationChangedEventArgs args)
{
position = args.Position;
}
//gps設(shè)備狀態(tài)改變時更新設(shè)備的狀態(tài)
void gps_DeviceStateChanged(object sender, DeviceStateChangedEventArgs args)
{
device = args.DeviceState;
}
//菜單:打開GPS設(shè)備
private void open_gps_Click(object sender, EventArgs e)
{
if (!gps.Opened)
{
gps.Open();
}
open_gps.Enabled = false;
close_gps.Enabled = true;
}
//菜單:關(guān)閉GPS設(shè)備
private void close_gps_Click(object sender, EventArgs e)
{
if (gps.Opened)
{
gps.Close();
}
open_gps.Enabled = true;
close_gps.Enabled = false;
}
//菜單:退出
private void exit_Click(object sender, EventArgs e)
{
if (gps.Opened)
gps.Close();
Close();
}
//獲取經(jīng)緯度坐標值
private void getdata_Click(object sender, EventArgs e)
{
string str;
if (!gps.Opened)
{
str = "GPS設(shè)備沒有打開,請點擊打開GPS菜單后重試!";
MessageBox.Show(str);
return;
}
if (device == null)
{
str = "GPS設(shè)備打開錯誤,請重新插拔GPS卡后重試!";
MessageBox.Show(str);
return;
}
if (position != null)
{
string strJd; //經(jīng)度
string strWd; //緯度
strJd = position.DoubleLongtitude.ToString();
strWd = position.DoubleLatitude.ToString();
DialogResult result;
str = "經(jīng)度:" +strJd + "\n緯度:" + strWd;
result = MessageBox.Show(str, "當前坐標", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
if (result == DialogResult.OK)
{
//將經(jīng)過確認的坐標數(shù)據(jù)顯示到相應的TextBox
wd.Text = strWd;
jd.Text = strJd;
return;
}
}
}
(完)