Abstract:
This article explains the GPS application based on Windows Mobile 5.0 from the perspective of a newbie who has never been familiar with mobile platform development.ProgramDevelopment Process. Use Visual C # To develop mobile applications efficiently.
Development Platform:
Operating System: Window XP
Development Environment:
Visual Studio 2005
Windows Mobile 5.0 Pocket PC SDK
. NET Compact Framework 2.0 (included in vs2005)
ActiveSync 4.0
Mobile devices:
Dell x51 PDA + GPS card
- Environment Construction
1) install visual stuido 2005
2) install activesync4.0 (or an updated version)
3) install the Windows Mobile 5.0 Pocket pc sdk (wm2003sdk is installed by default in vs2005, so you need to manually install the wm5 SDK)
You can download all the above files on the Internet. The installation process should be relatively simple and there are no complicated settings. If you do not understand the installation process, you can send an e-mail consultation.
- Detailed steps
1) Start vs2005. the first startup will prompt you to set the default development mode. You can select Visual C #.
2) Click [file]-> [new project]
:
A) project type: Select Visual C # à smart device à Windows Mobile 5.0 Pocket PC (If this option is not available, wm5.0sdk is not installed or installation fails)
B) template: select the device application.
C) enter the name, location, solution name, and other information and click OK.
3) Click [file] à add à existing project
Find the. \ Program Files \ Windows CE tools \ wce500 \ Windows Mobile 5.0 Pocket pc sdk \ samples \ CS \ GPS to find the Microsoft. windowmobile. samples. Location. csproj project file,
This project encapsulates some API functions for accessing the GPS hardware. It is very convenient to use. If this file is not found, check whether wm5.0sdk is installed.
After opening it, you can add it to an existing project, as shown in:
4) Set Project Dependencies
Click [project]-> Project dependency
Because you want to reference the added project in the testgps project, the testgps project depends on Microsoft. windowsmobile. samples. location.
The order of generation is naturally after testgps.
5) Add a project reference
Click [project] to add reference
Select the [project] Page, select the current project, and click OK. The reference to the project is displayed in the reference list of the testgps project.
Add a reference to the project class package.
6) Description
After the class package is introduced, we can reference the encapsulated class in the program to access GPS. In the project, we can see several common classes:
Degreesminutesseconds. CS // converts the coordinates of the longitude and latitude to the second.
Devicestatechangedeventargs. CS // events triggered when the status of the GPS device changes
GPS. CS // class for GPS operations, mainly responsible for open () and close () GPS devices.
Several statuses of gpsdevicestate. CS // GPS devices
Gpsposition. CS // class for processing latitude and longitude coordinates.
Locationchangedeventargs. CS // event triggered when the location changes (I .e., the latitude and longitude coordinates change)
It must be noted that when I use the longpolling and latitude attributes of the gpsposition class to obtain the longitude and latitude coordinates, the exception of dividedbyzeroexception always occurs. it was observed that an error occurred while converting the longitude and latitude coordinate values in the degree minute and second format to the decimal degrees expression format (readCodeAfter that, you will understand it better than what I said, so you don't have to worry about it because my statement is not very clear !), What I need is actually the original longitude and latitude coordinate value of the double type, which does not need to be converted. So I made a simple modification to the gpsposition class to meet my needs.
Add several lines of code at the end of the gpspositon class.
code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/
--> Public double doublelatitude
{< br> Get { return dbllatitude ;}
}< br> Public double doublelongtitude
{< br> Get { return dbllongpolling ;}
}< br>
The above mentioned only reminds beginners who may have the same requirements as me to avoid detours.
- ReferencesSource code:
Code
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 device status object
Gpsdevicestate Device = Null ;
// GPS location object
Gpsposition position = Null ;
// GPS object
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 );
}
// Update coordinate data when location changes
Protected Void Gps_locationchanged ( Object Sender, locationchangedeventargs ARGs)
{
Position = Args. position;
}
// Update the device status when the status of the GPS device changes
Void Gps_devicestatechanged ( Object Sender, devicestatechangedeventargs ARGs)
{
Device = Args. devicestate;
}
// Menu: Turn on the GPS device
Private Void Open_gps_click ( Object Sender, eventargs E)
{
If ( ! GPS. Opened)
{
GPS. open ();
}
Open_gps.enabled = False ;
Close_gps.enabled = True ;
}
// Menu: Disable GPS devices
Private Void Close_gps_click ( Object Sender, eventargs E)
{
If (GPS. Opened)
{
GPS. Close ();
}
Open_gps.enabled = True ;
Close_gps.enabled = False ;
}
// Menu: Exit
Private Void Exit_click ( Object Sender, eventargs E)
{
If (GPS. Opened)
GPS. Close ();
Close ();
}
// Obtain latitude and longitude coordinate values
Private Void Getdata_click ( Object Sender, eventargs E)
{
String STR;
If ( ! GPS. Opened)
{
Str = " The GPS device is not enabled. Please click to open the GPS menu and try again! " ;
MessageBox. Show (STR );
Return ;
}
If (Device = Null )
{
Str = " An error occurred while logging on the GPS device. Unplug the gpscard and try again! " ;
MessageBox. Show (STR );
Return ;
}
If (Position ! = Null )
{
String Strjd; // Longitude
String Strwd; // Latitude
Strjd = Position. doublelongtitude. tostring ();
Strwd = Position. doublelatitude. tostring ();
Dialogresult result;
Str = " Longitude: " + Strjd + " \ N latitude: " + Strwd;
Result = MessageBox. Show (STR, " Current Coordinate " , Messageboxbuttons. okcancel, messageboxicon. Question, messageboxdefaultbutton. button2 );
If (Result = Dialogresult. OK)
{
// Display the confirmed coordinate data to the corresponding textbox
WD. Text = Strwd;
JD. Text = Strjd;
Return ;
}
}
}
Hope to help beginners!