Sunday, March 11, 2007

Due to lots and lots of email requesting the code to send your location to a named contact,   I thought I would oblige and post the code.    I've adapted the GPS sample that ships with the Windows Mobile 6 SDK to include a send location option as shown below:

 

 

The code behind the button is very straight forward using the Windows Mobile POOM integration.   The user is prompted for a contact and then sends an email.    The email is sent using the default Outlook account (easy to change if using another service.

So here's the added code -

 

private void mnusendlocation_Click(object sender, EventArgs e)
{
GpsPosition pos = gps.GetPosition();
if (!(pos.LatitudeValid || pos.LongitudeValid))
{
DialogResult ret = MessageBox.Show("Position Is Not Currently Valid, Send Anyway?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
if (ret == DialogResult.No) return;

}
Microsoft.WindowsMobile.Forms.ChooseContactDialog c = new Microsoft.WindowsMobile.Forms.ChooseContactDialog();
DialogResult dr = c.ShowDialog();
if (dr != DialogResult.OK)
{
c.Dispose();
return;
}

string emailaddr = c.SelectedContact.Email1Address;
c.Dispose();

EmailMessage message = new EmailMessage();
message.Subject = "My Current Location"

message.BodyText = "http://maps.live.com/default.aspx?v=2&cp="+pos.Latitude+"~"+pos.Longitude+"&style=r&lvl=14&tilt=-90&dir=0&alt=-1000"

Recipient client = new Recipient(emailaddr);
message.To.Add(client);

OutlookSession currentSession = new OutlookSession();
currentSession.EmailAccounts[0].Send(message);
MessageBox.Show("Message sent!");

}

 

Download Source Here

Theme design by Jelle Druyts

Pick a theme: