摘要:
本文從一個完全沒接觸過移動平台開發的新手的角度講解基於Windows Mobile 5.0平台的GPS應用程式的開發過程.體驗使用Visual C#開發行動裝置 App程式的高效率.
開發平台:
作業系統: Window XP
開發環境:
Visual Studio 2005
Windows Mobile 5.0 Pocket PC SDK
.Net Compact Framework 2.0 (VS2005內建)
ActiveSync 4.0
行動裝置:
Dell X51 PDA + GPS 卡
- 環境的搭建
1) 安裝Visual Stuido 2005
2) 安裝ActiveSync4.0(或更新版本)
3) 安裝Windows Mobile 5.0 Pocket PC SDK(VS2005預設安裝WM2003SDK,所以需要手動安裝WM5的SDK)
以上所需在網上均可找到下載,安裝過程應該比較簡單,沒有什麼複雜的設定所以略過不談.有不明白的可以發E-Mail諮詢.
- 詳細步驟
1) 啟動VS2005.第一次啟動會提示設定預設開發模式,可以選擇Visual C#.
2) 點擊 [檔案]->[建立項目]
:
a) 項目類型:選擇Visual C#à智慧型裝置àWindows Mobile 5.0 Pocket PC(如果沒有該選項則說明沒有安裝WM5.0SDK或者安裝失敗)
b) 模板:選擇裝置應用程式即可
c) 輸入名稱,位置,解決方案名稱等資訊後點擊確定即可.
3) 點擊[檔案]à添加à現有項目
找到..\Program Files\Windows CE Tools\wce500\Windows Mobile 5.0 Pocket PC SDK\Samples\Cs\Gps即可找到Microsoft.WindowMobile.Samples.Location.csproj專案檔,
該項目封裝了訪問GPS硬體的一些API函數.使用非常方便.沒有找到該檔案的話請確認是否安裝了WM5.0SDK.
開啟後即可添加到現有項目中.如示:
4) 設定項目依賴性
點擊[項目]->項目依賴性
因為要在TestGPS項目中引用添加的項目,所以TestGPS項目依賴於Microsoft.WindowsMobile.Samples.Location
產生順序自然就是TestGPS在後了.
5) 添加項目引用
點擊[項目]添加引用
選擇[項目]頁,選擇當前項目後確定.即可在TestGPS項目的引用列表中看到對該項目的引用.
添加對項目類包的引用.
6)說明
在引入了類包之後,我們就可以在程式中引用已經封裝好的類來訪問GPS了.在項目中我們可以看到常用的幾個類:
DegreesMinutesSeconds.cs //主要負責經緯度座標度分秒的轉換
DeviceStateChangedEventArgs.cs //GPS裝置狀態改變時觸發的事件
GPS.cs //操作GPS的類,主要有負責Open()和Close()GPS裝置.
GpsDeviceState.cs //GPS裝置的幾種狀態
GpsPosition.cs //處理經緯度座標的類.
LocationChangedEventArgs.cs //位置改變時觸發的事件(即經緯度座標發生變化)
需要說明的是,我在使用GpsPosition類的Longitude和Latitude屬性擷取經緯度座標的時候總是出現DividedByZeroException的例外.經過觀察發現是由於對度分秒格式的經緯度座標值轉化為Decimal Degrees表達形式的時候出錯(看了代碼之後大家理解的會比我說的更明白,所以看不明白這一點的不必介意因為我的表述也不是很清楚!),而我需要的其實就是最原始的double類型的經緯度座標值,不需要進行任何轉換即可.所以我對GpsPosition類進行了簡單的修改以滿足我的需要.
在GpsPositon類的末尾加入一下幾行代碼.
public double DoubleLatitude
{
get { return dblLatitude; }
}
public double DoubleLongtitude
{
get { return dblLongitude; }
}
以上提到這些只是為可能會和我有同樣需求的初學的網友提個醒,免得走彎路.
- 附參考原始碼:
代碼
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裝置狀態物件
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);
}
//位置改變時更新座標資料
protected void gps_LocationChanged(object sender, LocationChangedEventArgs args)
{
position = args.Position;
}
//gps裝置狀態改變時更新裝置的狀態
void gps_DeviceStateChanged(object sender, DeviceStateChangedEventArgs args)
{
device = args.DeviceState;
}
//菜單:開啟GPS裝置
private void open_gps_Click(object sender, EventArgs e)
{
if (!gps.Opened)
{
gps.Open();
}
open_gps.Enabled = false;
close_gps.Enabled = true;
}
//菜單:關閉GPS裝置
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();
}
//擷取經緯度座標值
private void getdata_Click(object sender, EventArgs e)
{
string str;
if (!gps.Opened)
{
str = "GPS裝置沒有開啟,請點擊開啟GPS菜單後重試!";
MessageBox.Show(str);
return;
}
if (device == null)
{
str = "GPS裝置開啟錯誤,請重新插拔GPS卡後重試!";
MessageBox.Show(str);
return;
}
if (position != null)
{
string strJd; //經度
string strWd; //緯度
strJd = position.DoubleLongtitude.ToString();
strWd = position.DoubleLatitude.ToString();
DialogResult result;
str = "經度:" +strJd + "\n緯度:" + strWd;
result = MessageBox.Show(str, "當前座標", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
if (result == DialogResult.OK)
{
//將經過確認的座標資料顯示到相應的TextBox
wd.Text = strWd;
jd.Text = strJd;
return;
}
}
}
希望對初學者有所協助!