Windows Phone code sample : showing current location on map and sharing it using Email
In this post, we will create a simple Windows Phone app which uses Bing map control. We will then locate our current location on the Bing map. The app will also have a option to share the current location (Latitude and Longitude) using Email Launcher.
1. Lets get started by creating a new Windows Phone Application from the Visual Studio File Menu. We are using Visual C# - Silverlight for Windows Phone Template for this project.
2. Now drag the Map control from Windows Phone Control Toolbox on the page. Stretch and Scale the Map as per your requirements.
3. We want to have the ZoomBar visible on the map control, for which you need to add the following line of code to the Constructor. Here map1 is the name of the map control we are using.
public MainPage()
{
InitializeComponent();
map1.ZoomBarVisibility = Visibility.Visible;
}
If you run the app, you will see a map with the ZoomBar. Our next task is to locate the current co-ordinates on this map.
4. For using the GeoCoordinateWatcher class, you will need to include System.Device.Location namespace:
using System.Device.Location;
5. Initialize the object
private GeoCoordinateWatcher loc = null;
6. Now we will add a button, clicking which will take us to our current location on the map. Lets name this button btnMe and change the content to “Me”.
7. We will now add code for button click event:
private void btnMe_Click(object sender, RoutedEventArgs e)
{
loc = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
//EventHandler for location service status changes
loc.StatusChanged += loc_StatusChanged;
//If the location service is disabled or not supported
if (loc.Status == GeoPositionStatus.Disabled)
{
//Display a message
MessageBox.Show("Location services must be enabled");
return;
}
loc.Start();
}
8. Move and zoom the map view to current location
void loc_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
if (e.Status == GeoPositionStatus.Ready)
{
map1.SetView(loc.Position.Location, 10.0);
loc.Stop();
}
}
9. Now that we have the map showing our current location, lets add code to Email our coordinates. First of all we will add another button, name it btnEmail and change the content to “Email”. Your page should now look like the one shown below:
10. Add the namespace Microsoft.Phone.Tasks to use EmailCompose Task
using Microsoft.Phone.Tasks;
11. Add the following code to button Click event
private void btnEmail_Click(object sender, RoutedEventArgs e)
{
string Msg = "Lat=" + loc.Position.Location.Latitude.ToString();
Msg=Msg + " Long=" + loc.Position.Location.Longitude.ToString();
EmailComposeTask emailcomposer = new EmailComposeTask();
emailcomposer.To = "someone@somedomain.com";
emailcomposer.Subject = "My current location";
emailcomposer.Body = Msg;
//Show the Email application
emailcomposer.Show();
}
Your application is now ready
Testing your application
You can test your app on the Emulator by pressing F5. Once your app is up on the Emulator, you can simulate your current location by using the additional tools on the emulator. Goto the Location tab and select a location to simulate.
Related Resources