Click here to Skip to main content
Click here to Skip to main content

Windows Phone : Location : Map : Pushpin

, 12 Aug 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Track your location, mark it on a map using pushpin

Introduction

Location services are most commonly used in the mobile apps these days and it is necessary also, like for a restaurant app it is necessary to know where you are exactly before it starts suggesting you the options you can explore, without the use of location services the most successful apps today might have failed. In this post we will start learning how to use location service and maps in your windows phone application.

Background

GeoLocation depends a lot on how you are running it though GPS is considered the most accurate option but consumes the battery faster and responds a bit slower, the other option we have is the cell data but again the issue with this is the accuracy i.e. it is not much accurate, the best results are experienced over WiFi or the new 4G mobile networks. In windows phone we can adjust the accuracy of the location services we are using, the options available to us are : 

  • PositionAccuracy.High - this provides the most accurate results but at the cost of increased battery use.
  • PositionAccuracy.Default - this provides the results with optimal power.
  • DesiredAccuracyInMeters - in this we can specify the value in meters the amount of accuracy we require in our app. (I preferably use this method only)

Getting Your Current Location

Before getting started with the code we need to add location capability to your app, for this goto WMAppManifest.xml under properties of your application.

Capabilities

using Windows.Devices.Geolocation;
private async void GetLocation(object sender, RoutedEventArgs e)
        {
            Geolocator gl= new Geolocator();
            gl.DesiredAccuracyInMeters = 50;
            try
            {
                Geoposition gp = await gl.GetGeopositionAsync(
                    maximumAge: TimeSpan.FromMinutes(5),
                    timeout: TimeSpan.FromSeconds(10));
                string lat = gp.Coordinate.Latitude.ToString("0.00");
                string longi = gp.Coordinate.Longitude.ToString("0.00");
                MessageBox.Show("Latitude: " + lat + " Longitude: " + longi);
            }
            catch (UnauthorizedAccessException)
            {
                MessageBox.Show("location services disabled on phone");
            }
        }

We fist declared the GeoLocator object and mentioned the DesiredAccuracyInMeters, then we adjusted the parameters for GeoPosition i.e. maximumAge and timeout, maximumAge means the maximum acceptable age of cached location data and timeout is the maximum time allowed to get the location data else the exception occurs. GeoPosition.Coordinate gives the coordinate of the location captured.

Output

coordinate 

Plotting Location On Map

Now we have got the location lets see how we can plot it on a map, we will follow a three simple step process to do so.

  1. Add required namespaces.
    using Microsoft.Phone.Maps.Controls;
    using System.Device.Location; 
  2. Add map control to your app.
    ​Map MyMap = new Map();
    ContentPanel.Children.Add(MyMap);
  3. Set the map centre and adjust zoom level accordingly. (ZoomLevel value can vary from 1 to 20)
    MyMap.Center = new GeoCoordinate(gp.Coordinate.Latitude, gp.Coordinate.Longitude);
    MyMap.ZoomLevel = 10;

Output

My Location

Pushpin

As you see in the above output we are not able to see where exactly is our location in the map shown on the screen, pushpins are the icons used to mark positions on the map. So lets add a pushpin to the map and make our position on map clear. We can create a custom pushpin also and add a layer to the map, in this example we will use the standard pushpin available in the Windows Phone Toolkit. So our first task is to add the Windows Phone Toolkit to the project using Nuget packages.

Nuget

WP Toolkit

Once you have added the Windows Phone Toolkit to your application you can start coding for Pushpin.

  1. Adding Namespaces.
    using Microsoft.Phone.Maps.Toolkit; 
  2. Creating Pushpin
    Pushpin myPushpin = new Pushpin();
    myPushpin.Content = "My Location"; 
  3. Creating MapOverlay, Adding Content, Adjusting Coordinate
    MapOverlay MyOverlay = new MapOverlay();
    MyOverlay.Content = myPushpin;
    MyOverlay.GeoCoordinate = new GeoCoordinate(gp.Coordinate.Latitude, gp.Coordinate.Longitude);
    MyOverlay.PositionOrigin = new Point(0, 0.5); 
  4. Adding MapOverlay to MapLayer and MapLayer to you Map
    MapLayer MyLayer = new MapLayer();
    MyLayer.Add(MyOverlay);
    MyMap.Layers.Add(MyLayer);
    

Output

Pushpin

Now the output looks clear. This is a standard pushpin and in the next article we will learn to build a custom pushpin.

Source Code.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

ShubhamSaxena
Software Developer Accenture
India India
I'm Software Engineer Associate at Accenture Services Pvt. Ltd. and I love Microsoft Technologies.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150129.1 | Last Updated 12 Aug 2014
Article Copyright 2014 by ShubhamSaxena
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid