Converting To Decimal Degrees From Any Spatial Reference

3900
1
06-28-2012 06:38 AM
by Anonymous User
Not applicable
I've seen many posts online where people are looking for a solution to convert your silverlight/flex/java script map service from its current units to decimal degrees (latitude and longitude). I have that solution after spending many hours trying to figure this out. The C# code comes directly from ESRI, the VB.Net is my code. This is the proper way to complete this task. Since I use VB.Net the code will look a little different because I have either added more coordinates to be displayed or I customized my ListBox. The format that I currently have the code in is Silverlight VB.Net & C#, for Flex or Java script you will just need to convert the code to suit your specific language you are using. I will be posting both C# and VB.Net code. I hope this will clear up many unanswered questions and solve many of your issues.

Silverlight C# XAML:

<UserControl xmlns:esri="http://schemas.esri.com/arcgis/client/2009"  x:Class="zoomToPoint.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <esri:Map Name="myMap" MouseMove="myMap_MouseMove">
            <esri:ArcGISTiledMapServiceLayer ID="myTiled"
                                             Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/>
        </esri:Map>

        <Grid Width="355" Height="65" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,15,0" >
            <Rectangle Stroke="Gray"  RadiusX="10" RadiusY="10" Fill="#77919191" Margin="0,0,0,6" >
                <Rectangle.Effect>
                    <DropShadowEffect/>
                </Rectangle.Effect>
            </Rectangle>
            <Rectangle Fill="#FFFFFFFF" Stroke="DarkGray" RadiusX="5" RadiusY="5" Margin="10,10,10,15"  />
            <StackPanel Canvas.Left="20" Canvas.Top="14" Margin="20,14,10,0">
                <TextBlock x:Name="ScreenCoordsTextBlock"
                    HorizontalAlignment="Left" VerticalAlignment="Center" Text="Screen Coords: " TextWrapping="Wrap" FontWeight="Bold" />
                <TextBlock x:Name="MapCoordsTextBlock"
                    HorizontalAlignment="Left" VerticalAlignment="Center" Text="Map Coords: " TextWrapping="Wrap" FontWeight="Bold" />
            </StackPanel>
        </Grid>
    </Grid>
</UserControl>

Silverlight C#:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Tasks;
using ESRI.ArcGIS.Client;

namespace zoomToPoint
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void myMap_MouseMove(object sender, System.Windows.Input.MouseEventArgs args)
        {
           
            if (myMap.Extent != null)
            {
                System.Windows.Point screenPoint = args.GetPosition(myMap);
                ScreenCoordsTextBlock.Text = string.Format("Screen Coords: X = {0}, Y = {1}",
                    screenPoint.X, screenPoint.Y);

                ESRI.ArcGIS.Client.Geometry.MapPoint mapPoint = myMap.ScreenToMap(screenPoint);

                ESRI.ArcGIS.Client.Graphic grpc = new ESRI.ArcGIS.Client.Graphic();
                grpc.Geometry = mapPoint;

                GeometryService geometryService = new GeometryService("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
                geometryService.ProjectCompleted += GeometryService_ProjectCompleted;
                geometryService.Failed += GeometryService_Failed;
                List<Graphic> graphicList = new List<Graphic>();
                graphicList.Add(grpc);
                geometryService.ProjectAsync(graphicList, new SpatialReference(4326));

             }
           
        }
       
        private void GeometryService_ProjectCompleted(object sender, ESRI.ArcGIS.Client.Tasks.GraphicsEventArgs args)
        {
            ESRI.ArcGIS.Client.Geometry.MapPoint mpt = new MapPoint();
            mpt = (ESRI.ArcGIS.Client.Geometry.MapPoint) args.Results[0].Geometry;
            MapCoordsTextBlock.Text = string.Format("Map Coords: X = {0}, Y = {1}", Math.Round(mpt.X, 2), Math.Round(mpt.Y, 2));
        }
        private void GeometryService_Failed(object sender, TaskFailedEventArgs e)
        {
            MessageBox.Show("Geometry Service error: " + e.Error);
        }
           
    }
}
Screenshot of the C# ESRI Sample:

[ATTACH=CONFIG]15567[/ATTACH]



Silverlight VB.Net XAML:


<UserControl xmlns:esri="http://schemas.esri.com/arcgis/client/2009
    x:Class="StreetLights_With_Silverlight.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <esri:Map x:Name="MyMap" WrapAround="True" MouseMove="MyMap_MouseMove" IsLogoVisible="False">
                <esri:Map.Layers>
                         <esri:ArcGISDynamicMapServiceLayer ID="LightsMapService" Visible="True"
                                                   Url="http://server/ArcGIS/rest/services/Silverlight_Light_Poles/MapServer"
                                                   Initialized="ArcGISDynamicMapServiceLayer_Initialized"/>
                 </esri:Map.Layers>
         </esri:Map>

         <!--ListBox where the map coordinates will show when mouse is moved-->
         <Border Background="#99919191" BorderThickness="1" CornerRadius="5"
                HorizontalAlignment="Right" VerticalAlignment="Bottom"
                Margin="8" Padding="1" BorderBrush="Black" >

            <Grid HorizontalAlignment="Right" VerticalAlignment="Top" >
            <Grid.RowDefinitions>
                <RowDefinition Height="1"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="1"/>
            </Grid.RowDefinitions>
            <ListBox Background="White" Margin="5" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Row="1">
                    <!--The reason I have 4 TextBlocks is because I wanted to see both Latitude & Longitude and my original map units(Meters)
                    <TextBlock x:Name="DecDegX" Text="Latitude: NaN" HorizontalAlignment="Left" VerticalAlignment="Center" TextWrapping="Wrap" FontWeight="Bold" Grid.Row="2" />
                    <TextBlock x:Name="DecDegY" Text="Longitude: NaN" HorizontalAlignment="Left" VerticalAlignment="Center" TextWrapping="Wrap" FontWeight="Bold" Grid.Row="2"/>
                    <TextBlock x:Name="XCor" Text="X: NaN" HorizontalAlignment="Left" VerticalAlignment="Center" TextWrapping="Wrap" FontWeight="Bold" Grid.Row="1"/>
                    <TextBlock x:Name="YCor" Text="Y: NaN " HorizontalAlignment="Left" VerticalAlignment="Center" TextWrapping="Wrap" FontWeight="Bold" Grid.Row="2"/>
                </ListBox>
            </Grid>
            </Border>

Silverlight VB.Net:

Private Sub MyMap_MouseMove(ByVal sender As Object, ByVal args As System.Windows.Input.MouseEventArgs)
        Try

            If MyMap.Extent IsNot Nothing Then

                Dim screenPoint As System.Windows.Point = args.GetPosition(MyMap)
                Dim mapPoint As ESRI.ArcGIS.Client.Geometry.MapPoint = MyMap.ScreenToMap(screenPoint)
                XCor.Text = String.Format("X: {0} Meters", Math.Round(mapPoint.X, 6))
                YCor.Text = String.Format("Y: {0} Meters", Math.Round(mapPoint.Y, 6))


                Dim mapPointAsGraphic As New ESRI.ArcGIS.Client.Graphic
                mapPointAsGraphic.Geometry = mapPoint

                Dim geoServ As GeometryService = New GeometryService("http://server/ArcGIS/rest/services/Geometry/GeometryServer")
                AddHandler geoServ.ProjectCompleted, AddressOf GeometryService_ProjectCompleted
                AddHandler geoServ.Failed, AddressOf GeometryService_ProjectFailed

                Dim graphicL As New List(Of Graphic)
                graphicL.Add(mapPointAsGraphic)
                geoServ.ProjectAsync(graphicL, New SpatialReference(4326))

            End If

        Catch ex As System.NullReferenceException

        End Try
    End Sub

    Private Sub GeometryService_ProjectCompleted(ByVal sender As Object, ByVal args As ESRI.ArcGIS.Client.Tasks.GraphicsEventArgs)
        Dim newMP As ESRI.ArcGIS.Client.Geometry.MapPoint = New MapPoint
        newMP = TryCast(args.Results(0).Geometry, ESRI.ArcGIS.Client.Geometry.MapPoint)
        DecDegX.Text = String.Format("Latitude:  {0}°", Math.Round(newMP.X, 2))
        DecDegY.Text = String.Format("Longitude: {0}°", Math.Round(newMP.Y, 2))
    End Sub

    Private Sub GeometryService_ProjectFailed(ByVal sender As Object, ByVal e As TaskFailedEventArgs)
        MessageBox.Show("Geometry Service error: " & e.Error.Message)
    End Sub


Screenshot VB.Net Latitude/Longitude

[ATTACH=CONFIG]15568[/ATTACH]
0 Kudos
1 Reply
by Anonymous User
Not applicable
Original User: dbroux

Thanks for sharing.

Additionaly you can format your coordinates by using this Morten's post : http://www.sharpgis.net/post/2011/11/20/Correctly-displaying-your-current-location.aspx
0 Kudos