Programmatically Get Current Location in windows phone

Programmatically Get Current Location in windows phone

This article will explain how to get current location of phone continuously.

Include the ID_CAP_LOCATION capability in app manifest file


Create a new Windows Phone App and then select Windows Phone 8.0 as platform you want to target

Go to Solution Explorer, expand the Properties folder, and then double-click WMAppManifest.xml

Click on the Capabilities tab of the manifest designer, select the check box next to ID_CAP_LOCATION



<!--TitlePanel contains the name of the application and page title-->

        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">

            <TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>

            <TextBlock Text="Location" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>



        <!--ContentPanel - place additional content here-->

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

            <Button Content="Get Current Location" Height="72" HorizontalAlignment="Left" Margin="9,6,0,0" Name="Location" VerticalAlignment="Top" Width="441" Click="Location_Click"    />

            <TextBlock Height="62" HorizontalAlignment="Left" TextWrapping="Wrap" Margin="29,108,0,0" Name="txtLat" VerticalAlignment="Top" Width="407" />

            <TextBlock Height="62" HorizontalAlignment="Left" TextWrapping="Wrap" Margin="29,175,0,0" x:Name="txtLong" VerticalAlignment="Top" Width="407" />





First add the using statement


using System.Device.Location;


//Declare the instance of the GeoCoordinator class as a global variable


GeoCoordinateWatcher gcw;


private void Location_Click(object sender, RoutedEventArgs e)


            gcw = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);

            gcw.PositionChanged += gcw_PositionChanged;




        void gcw_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)


           txtLat.Text = "Latitude = " +  e.Position.Location.Latitude.ToString();

            txtLong.Text = "Longitude = " +  e.Position.Location.Longitude.ToString();






Hope it helps. 
comments powered by Disqus

Related Articles