Show your current location on Bing map in Windows Phone 7

In this article you will see how you can show your current location on Bing map in Windows Phone7


Objective of this article is to get current latitude and longitude of your phone using Location API and passing it to the Map control to show your exact location on the map. As every Windows 7 phone is equipped with an inbuilt GPS, we can easily its coordinates with high accuracy.

Step 1:

Create a new Windows Phone Application.

Step 2:

Drag a Map control on MainPage.xaml and adjust it to fit the Grid. Register for a Bing Maps API key at and set the value of CredentialsProvider attribute with your key.

<my:Map Name="map1" CredentialsProvider="Your Map Key" Width="438" Height="595" HorizontalAlignment="Left" VerticalAlignment="Top"/>

Step 3:

To use Location service, we need to add a reference to System.Device dll and include System.Device.Location namespace


using System.Device.Location;

GeoCoordinateWatcher myCoordinateWatcher;
double Latitued, Longitude;

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
     Latitued = 28.3131;
     Longitude = 77.1212;

     map1.Center = new GeoCoordinate(Latitued, Longitude);
     map1.ZoomLevel = 5;
     map1.ZoomBarVisibility =Visibility.Visible;
     myCoordinateWatcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
     myCoordinateWatcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(myCoordinateWatcher_PositionChanged);

Note: To add PositionChanged event, just type myCoordinateWatcher.PositionChanged += and press TAB key twice. Visual Studio will create event for you.

void myCoordinateWatcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
     if (!e.Position.Location.IsUnknown)
          Latitued = e.Position.Location.Latitude;
          Longitude = e.Position.Location.Longitude;
          map1.Center = new GeoCoordinate(Latitued, Longitude);

Code Explanation:

System.Device.Location namespace is added to use Location API. A GeoCordinateWatcher and two double variables are decelared inside class declaration. In the application load event default values for Latitude and Longitude are set. Map1.Center is used to set the map center to the given coordinates. Map1.ZoomLevel=5 is used to set zoom level of the map. Map1.ZoomBarVisibility is used to show Zoom In and Zoom out button. GeoCoordinateWatcher is used to get current Latitude and Longitude of the device. GeoPositionAccuracy.High is used to ensure that we will get coordinates with high accuracy. PositionChanged event of GeoCoordinateWatcher gets fired when phone is moved to a different location. In the myCoordinateWatcher_PositionChanged if Location is known then current Latitude and Longitude is retrieved and map is centered at that coordinates.

Unfortunately Location service only work with live device it does not give any default value in Emulator. So, I have used default coordinate to position map to India when the application loads.

Final Output:


About these ads

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s