📄 codeproject gps tracking with windows mobile 5_0+_ free source code and programming help.htm
字号:
<p>I would also suggest that you have a Windows Mobile 5.0 device to test the application with. You can use the emulators for testing the program. This program makes use of SMS messaging. Depending on your service plan with your phone company you may have to pay fees for ever message sent and received. I fall into this category, as at this point in time available carriers here in the United States do not allow many phones to have both unlimited text message subscriptions on the same phone line as a phone that has an unlimited data subscription. Because of this the emulators may be preferable for initial testing of the program. You can redirect the COM port of the emulated Windows Mobile device to an external GPS receiver connected to your PC or you can use the "FakeGPS" program available in the Windows Mobile 6.0 SDK to also emulate a GPS receiver. </p>
<h2>The Missing GPS Settings Icon</h2>
<p>On phones with Windows Mobile 5 Professional and later (the phones with touch sensitive screens) there is an icon in the Settings menu that allows one to set the hardware port over which their GPS receiver communicates. On some rather popular Windows Mobile phones this icon is missing. It is not erased. Rather some OEMs saw fit to hide the icon. The icon can be restored by deleting the registry keys [HKEY_LOCAL_MAHINE\ControlPanel\Settings\GPS Settings\Hide] and [HKEY_LOCAL_MAHINE\ControlPanel\Settings\GPS Settings\Redirect]. If you don't have a registry editor you can delete these keys by running the GPS Icon Restoration Program attached to this article. Simply copy it to your phone and run it. It will display a message box letting you know that it was successful or that those keys do not exists. You may need to reset your phone after running this program for the changes to take affect </p>
<h2>How does this program work</h2>
<p>When initially run the owner of the phone is able to enter a pin that will be used to identify location request messages. When an SMS message is received if it contains the PIN that the user selected this program will be started up (if it is not already running) and the message will be passed to the program. All other messages will be handled as usual on the phone. When the message is received the program reads the current GPS coordinates from the phone and responds back to the sender with a link to a map on local.live.com showing the phone owner's current location. </p>
<h3>Catching SMS messages with the MessageInterceptor class</h3>
<p>As it's name suggest the MessageInterceptor class is used to capture incoming SMS messages and allow your code to act upon receiving the message. Rather than needing to manually check all of the messages yourself you set up a message rule for the types of messages that you are interested in. Within this program there is only one property on the class that we have interest in; the MessageCondition member. </p>
<p>The MessageCondition member is of a class by the same name, MessageCondition. The MessageCondition allows you to specify a string that will be searched for in the message. You can specify that the string must be at the beginning, end, or anywhere within the message and you can specify whether or not the search is case sensitive. For this program I've set the MessageCondition class to only search within the body of the message for the presence of your pin anywhere in the message. </p>
<pre lang="cs">_messageInterceptor.MessageCondition = <span class="code-keyword">new</span> MessageCondition();
_messageInterceptor.MessageCondition.CaseSensitive = <span class="code-keyword">false</span>;
_messageInterceptor.MessageCondition.ComparisonType = MessagePropertyComparisonType.Contains;
_messageInterceptor.MessageCondition.ComparisonValue = <span class="code-keyword">this</span>.txtPin.Text;
_messageInterceptor.MessageCondition.Property = MessageProperty.Body;
</pre>
<p>Once the condition is set the MessageInterceptor only needs to be enabled and an event handler must be given to it </p>
<pre lang="cs">_messageInterceptor.EnableApplicationLauncher(ruleName);
_messageInterceptor.MessageReceived += _messageInterceptorEventHandler;
</pre>
<p>The user may have exited the program since the MessageInterceptor was created. If a message is received after exiting the program and if the user left the program in an Enabled state then the program will be automatically started. To catch messages that caused the program to start we must create the MessageInterceptor class in the program's load event. During load the program checks to see if the application had registered to be automatically loaded up receiving a message. If that is the case then the program creates a new MessageInterceptor and gives it an event handler. The application will then receive notification of that message through the event handler. </p>
<pre lang="cs"><span class="code-keyword">if</span> (MessageInterceptor.IsApplicationLauncherEnabled(ruleName))
{
_messageInterceptor = <span class="code-keyword">new</span> MessageInterceptor(ruleName);
_messageInterceptor.MessageReceived +=
<span class="code-keyword">new</span> MessageInterceptorEventHandler(_messageInterceptor_MessageReceived);
txtPin.Text = _messageInterceptor.MessageCondition.ComparisonValue;
<span class="code-keyword">this</span>.chkFindMeEnabled.Checked = <span class="code-keyword">true</span>;
}
<span class="code-keyword">else</span>
{
<span class="code-keyword">this</span>.chkFindMeEnabled.Checked = <span class="code-keyword">false</span>;
}
</pre>
<h3>Obtaining the current location</h3>
<p>The Intermediate GPS Driver handles the details of interfacing to the GPS hardware for us. I'm using a wrapper included in the Windows Mobile 6 SDK that simplifies the use of the driver. The code works well and is easy to use so I would encourage you to consider using it instead of creating your own GPS reading code. Initializing the GPS object can be done in a few lines of code </p>
<pre lang="cs">gps = <span class="code-keyword">new</span> Gps();
gps.LocationChanged += <span class="code-keyword">new</span> LocationChangedEventHandler(gps_LocationChanged);
gps.Open();
</pre>
<p>The EventArgs received from the LocationChanged event contain more details that you'll probably ever need from the GPS receiver. I simply save the Position member from the EventArgs and update the _currentLocation object. </p>
<pre lang="cs"><span class="code-keyword">void</span> gps_LocationChanged(<span class="code-keyword">object</span> sender, LocationChangedEventArgs args)
{
<span class="code-keyword">if</span> (args.Position.LatitudeValid && args.Position.LongitudeValid)
{
currentPosition = args.Position;
UpdatePosition();
}
}
</pre>
<p>When the program no longer needs use of the Gps Receiver a call to Gps.Close(); is made to end the objects worker thread. Not doing this could cause the program to never terminate. </p>
<h3>Sending a Message </h3>
<p>Sending messages is straight forward. In the case of the SMS message a new SMSMessage object is created with the receivers number and the message text as its constructor arguments. Then the objects Send() method is called to deliver the message. Since an SMS message cannot be more than 160 characters the link sent in an SMS message only contains a user's coordinates and a pushpin. </p>
<p>It would not be practical to receive the SMS message containing the map link on a phone so you may wonder why the program has this ability. You must remember that computers can also send SMS messages either through another application such as Live Messenger or through built in hardware such as a GMS modem or CDMA modem. </p>
<pre lang="cs"><span class="code-keyword">public</span> <span class="code-keyword">void</span> SmsSendCoordinates(<span class="code-keyword">string</span> to,GpsPosition pos)
{
<span class="code-keyword">string</span> message = <span class="code-SDKkeyword">String</span>.Format(responseTemplate, pos.Latitude, pos.Longitude);
SmsMessage sms = <span class="code-keyword">new</span> SmsMessage(to, message);
sms.Send();
<span class="code-keyword">this</span>.eventLog.Add(DateTime.Now, to, pos);
}
</pre>
<p>Sending e-mail messages is just as simple. Create a new Email object, populate a subject and body, add an address to the "To" collection, and then call it's Send() method. The Send() method for e-mail required an argument that was not needed for SMS. That argument is the name of the mail account to use. On my phone the available accounts are named "ActiveSync" and "MMS". Naturally I would rather not use MMS since my phone service provider charges what I feel to be high tarifs for sending a message via MMS. </p>
<p>Since the 160 character limit is not applied against e-mails I am at liberty to place more information in the link. So when sending an e-mail the program may (Depending on the user's selected options) include custom text and an image for representing the user. </p>
<pre lang="cs"> <span class="code-keyword">public</span> <span class="code-keyword">void</span> EmailSendCoordinates(<span class="code-keyword">string</span> emailAddress, GpsPosition pos)
{
<span class="code-keyword">string</span> avatarUrl = <span class="code-keyword">string</span>.Empty;
<span class="code-keyword">string</span> displayName = <span class="code-keyword">string</span>.Empty;
<span class="code-keyword">string</span> customMessage=string.Empty;
<span class="code-keyword">if</span>(((_Settings.OptionFlags&OptionChangedFlags.Avatar)==OptionChangedFlags.Avatar)&&
(_Settings.Avatar.Length>0)
)
avatarUrl=_Settings.Avatar;
displayName = optionsUI.DisplayName;
customMessage = optionsUI.CustomMessage;
<span class="code-keyword">string</span> message = <span class="code-SDKkeyword">String</span>.Format(detailedResponseTemplate, pos.Latitude, pos.Longitude,
Utility.UrlEncode(avatarUrl), Utility.UrlEncode
(displayName), Utility.UrlEncode(customMessage));
EmailAccount account = (<span class="code-keyword">new</span> OutlookSession()).EmailAccounts[_Settings.EmailAccount];
EmailMessage msg = <span class="code-keyword">new</span> EmailMessage();
msg.To.Add(<span class="code-keyword">new</span> Recipient(emailAddress));
msg.Subject = <span class="code-string">"</span><span class="code-string">My location"</span>;
msg.BodyText = message;
msg.Send(account);
<span class="code-keyword">this</span>.eventLog.Add(DateTime.Now, emailAddress, pos);
}
</pre>
<h2>What's Next</h2>
<p>There's quite a number of features I would like to add to this program. In the next revision I plan to add a mapping control so that users can see each others location from their phones. I may also add support for Windows Mobile 5 Standard Edition (the devices with no touch screens). To accomplish this there will beed to exists a web server. Once those two pieces are working I plan to use the Windows Live Search and MapPoint APIs to provide functionality for finding points of interest and getting directions to them so that users can meet each other.</p>
<h2>History</h2>
15 August 2007
<ul>
<li>Article Published </li>
</ul>
<!-- Article Ends -->
<!-- Main Page Contents End -->
</div>
</span>
<h2>License</h2>
<p>This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.</p><p>A list of licenses authors may to use can be found <a href="/info/Licenses.aspx">here</a></p>
<h2>About the Author</h2>
<table cellpadding="0" cellspacing="5" border="0" width="100%">
<tr valign="top">
<td id="ctl00_AboutAuthorRptr_ctl00_AboutAuthor_memberPhotoTable" valign="top" style="width:155px;">
<b><a id="ctl00_AboutAuthorRptr_ctl00_AboutAuthor_memberProfileLink" href="/script/Membership/Profiles.aspx?mid=621517">Joel Ivory Johnson</a></b><br /><br />
<center><img id="ctl00_AboutAuthorRptr_ctl00_AboutAuthor_memberPhoto" src="/script/Membership/ProfileImages/{427C259C-A033-4554-B582-BE77B7DCAEDF}.gif" style="border-width:0px;" /></center><br />
<span id="ctl00_AboutAuthorRptr_ctl00_AboutAuthor_memberType" class="SmallText"></span>
</td>
<td>
Attended Southern Polytechnic State University of Georgia, U.S.A. from 1996 to 2000. Received a degree of Computer Science (B.S.)<br /><br />Currently working on a Masters of Software Engineering with an expected graduation date of December 2008.<br />
<table>
<tr id="ctl00_AboutAuthorRptr_ctl00_AboutAuthor_jobTitleRow">
<td class="SmallText" nowrap="nowrap">Occupation: </td>
<td width="100%"><span id="ctl00_AboutAuthorRptr_ctl00_AboutAuthor_memberJobTitle" class="SmallText">Web Developer</span></td>
</tr>
<tr id="ctl00_AboutAuthorRptr_ctl00_AboutAuthor_locationRow">
<td class="SmallText">Location: </td>
<td width="100%"><span id="ctl00_AboutAuthorRptr_ctl00_AboutAuthor_memberLocation" class="SmallText"><img src="/script/Geo/Images/US.gif" alt="United States" width="16" height="11" /> United States</span></td>
</tr>
</table>
</td>
</tr>
</table>
<br />
<table id="ctl00_PopularArticlesRow" width="100%" cellpadding="0" cellspacing="0" border="0">
<tr valign="top">
<td style="width:100%;">
<h2>Other popular Mobile Development articles:</h2><ul><li><a href="/KB/mobile/WritingGPSApplications2.aspx">Writing Your Own GPS Applications: Part 2</a><div class="SmallText">In part two of the series, the author of "GPS.NET" teaches developers how to write GPS applications suitable for the real world by mastering GPS precision concepts. Source code includes a working NMEA interpreter and sample high-precision application in C# and VB.NET.</div></li><li><a href="/KB/mobile/tvremote.aspx">Pocket PC TV Remote Control</a><div class="SmallText">An article describing how to use the IR port on a pocket PC to control a TV.</div></li><li><a href="/KB/mobile/CfPocket1945.aspx">Pocket 1945 - A C# .NET CF Shooter</a><div class="SmallText">An article on Pocket PC game development</div></li><li><a href="/KB/mobile/irdamobile.aspx">Infrared Communication with your Mobile Phone</a><div class="SmallText">Learn how to make your Pocket PC speak with your mobile phone.</div></li><li><a href="/KB/mobile/PocketBeamer.aspx">C# Remote Control using the Audio Port</a><div class="SmallText">This article describes the development of C# code that allows you to send consumer IR codes from your mobile device using the audio port.</div></li></ul>
<h2></h2>
</td>
<td>
<script language="javascript">document.write("<a href=\"http://www.codeproject.com/Redir.aspx?adid=3484&way=ban\" target=\"_blank\" rel=\"nofollow\"><img src=\"http://www.codeproject.com/script/Ann/ServeImg.aspx?File=%2fscript%2fadmentor%2fimages%2fcp_daily_300x250.gif&C=False&id=3484\" alt=\"\" border=\"0\" width=\"300\" height=\"250\"></a>");</script>
</td>
</tr>
</table>
<div id="ctl00_AddTo" style="margin:10px">
<script language="JavaScript" type="text/javascript">
var addtoMethod=1;
var AddURL = document.location.href;
var AddTitle = escape(document.title);
DrawLinks(100, 0, "SmallText Bold", "AddTo")
</script>
</div>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -