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.
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);
<!--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