SHOW ON MAP MOUSE HOVER - LAT/LONG VALUES

5274
11
04-18-2011 04:05 AM
vipulsoni
Occasional Contributor
Hi,

I need help to show lat long on mouse hover on map. I have tried the existing code sample ---

  ESRI.ArcGIS.Client.Projection.WebMercator webmercator = new ESRI.ArcGIS.Client.Projection.WebMercator();           
                    ESRI.ArcGIS.Client.Geometry.MapPoint latlongpoint = (ESRI.ArcGIS.Client.Geometry.MapPoint)webmercator.FromGeographic(mapPoint);//here I have tried toGeographic also.. for just a try..but still got error ..
                    latlongTextBlock.Text = string.Format("Lat Long: X = {0}, Y = {1}", Math.Round(latlongpoint.Extent.XMin, 4), Math.Round(latlongpoint.Extent.YMin, 4));

But I kept on getting Invalid Spatial Reference error. My map wkid is 32640- wgs84 zone 40N projection.

therefore I added a line  -> mapPoint.SpatialReference.WKID = 32640; //but then again got the same spatial reference error..

Then I tried the GEOMETRY SERVICE approach explained in another post in this forum..

     Graphic pointGraphic = new Graphic();
                    pointGraphic.Geometry = mapPoint;
                    pointGraphic.Geometry.SpatialReference = new SpatialReference(32640);
                    IList<Graphic> inG = new List<Graphic>();
                    inG.Add(pointGraphic);
                   
                    GeometryService svcGeometry = new GeometryService(Service_URL.GEOMETRY_SUPREME);
                    svcGeometry.ProjectCompleted += new EventHandler<GraphicsEventArgs>(svcGeometry_ProjectCompleted);
                    svcGeometry.Failed += new EventHandler<TaskFailedEventArgs>(svcGeometry_Failed);
                    SpatialReference sr = new SpatialReference();
                    sr.WKID = 32640;
                    svcGeometry.ProjectAsync(inG, sr);

void svcGeometry_ProjectCompleted(object sender, GraphicsEventArgs e)
        {
            IList<Graphic> results = e.Results;
            Graphic projectedG = results[0]; //should just be one

            //**Code here to build new extent at which to center the map.   
            ESRI.ArcGIS.Client.Geometry.MapPoint newpoint = new MapPoint();
            newpoint.X = projectedG.Geometry.Extent.XMin;
            newpoint.Y = projectedG.Geometry.Extent.YMin;

            latlongTextBlock.Text = string.Format("Lat Long: X = {0}, Y = {1}", Math.Round(newpoint.X, 4), Math.Round(newpoint.Y, 4));
        }

But with this instead of getting lat longs ...I get the same results like from the following code sample -

ESRI.ArcGIS.Client.Geometry.MapPoint mapPoint = Map.ScreenToMap(screenPoint);                
                    MapCoordsTextBlock.Text = string.Format("Map Coords: X = {0}, Y = {1}",
                        Math.Round(mapPoint.X, 4), Math.Round(mapPoint.Y, 4));
    

My Map/datasets etc are in WGS_1984_UTM_Zone_40N Projection. I dont know where my approach is wrong here , please help...thanks in advance 😞
0 Kudos
11 Replies
ChristopherHill
Occasional Contributor
Your first atttempt with WebMercator. The WebMercator is a client side projection from "web mercator" to Geographic (4326 ) and back. You need to use the GeometryService projection and project 32640 to 4326. Looking at your geometry service projection it appears you are projecting from 32640 to 32640 which is the same thing as you started with. all you should need to do is reaplace the out spatial reference with 4326.
0 Kudos
vipulsoni
Occasional Contributor
Your first atttempt with WebMercator. The WebMercator is a client side projection from "web mercator" to Geographic (4326 ) and back. You need to use the GeometryService projection and project 32640 to 4326. Looking at your geometry service projection it appears you are projecting from 32640 to 32640 which is the same thing as you started with. all you should need to do is reaplace the out spatial reference with 4326.


Thanks Chris..
0 Kudos
BrandonCales
New Contributor III
Can anyone share a working code for this? I already have my mouse coordinates showing in xy, but want to give the user an option to change to lat/lon on the fly.
0 Kudos
vipulsoni
Occasional Contributor
Can anyone share a working code for this? I already have my mouse coordinates showing in xy, but want to give the user an option to change to lat/lon on the fly.


This is what I did .


#region Get Lat Longs
        /// <summary>
        /// Map_MouseMove
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void Map_MouseMove(object sender, System.Windows.Input.MouseEventArgs args)
        {          
            try
            {
                if (isMapLoaded & isEnabled)
                {
                    System.Windows.Point screenPoint = args.GetPosition(Map);
                    ScreenCoordsTextBlock.Text = string.Format("Screen Coords: X = {0}, Y = {1}",
                        screenPoint.X, screenPoint.Y);

                    ESRI.ArcGIS.Client.Geometry.MapPoint mapPoint = Map.ScreenToMap(screenPoint);
                    MapCoordsTextBlock.Text = string.Format("Map Coords: X = {0}, Y = {1}",
                        Math.Round(mapPoint.X, 4), Math.Round(mapPoint.Y, 4));

                    Graphic pointGraphic = new Graphic();
                    pointGraphic.Geometry = mapPoint;
                    pointGraphic.Geometry.SpatialReference = new SpatialReference(32640);
                    IList<Graphic> inG = new List<Graphic>();
                    inG.Add(pointGraphic);

                    GeometryService svcGeometry = new GeometryService(Service_URL.GEOMETRY_SUPREME);
                    svcGeometry.ProjectCompleted += new EventHandler<GraphicsEventArgs>(svcGeometry_ProjectCompleted);
                    svcGeometry.Failed += new EventHandler<TaskFailedEventArgs>(svcGeometry_Failed);
                    SpatialReference sr = new SpatialReference();
                    sr.WKID = 4326;
                    svcGeometry.ProjectAsync(inG, sr);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        void svcGeometry_Failed(object sender, TaskFailedEventArgs e)
        {
            MessageBox.Show(e.Error.Message);
        }

        void svcGeometry_ProjectCompleted(object sender, GraphicsEventArgs e)
        {
            IList<Graphic> results = e.Results;
            Graphic projectedG = results[0]; //should just be one

            latlongTextBlock.Text = string.Format("Lat Long: N = {0}, E = {1}", Math.Round(projectedG.Geometry.Extent.XMin, 4), Math.Round(projectedG.Geometry.Extent.YMin, 4));
        }

        private void startcoords_Click(object sender, RoutedEventArgs e)
        {
            if (startcoords.Content.ToString() == "Start Tool")
            {
                isEnabled = true;
                startcoords.Content = "Stop Tool";
            }

            else if (startcoords.Content.ToString() == "Stop Tool")
            {
                isEnabled = false;
                startcoords.Content = "Start Tool";
            }

        }
        #endregion
0 Kudos
BrandonCales
New Contributor III
Works perfect....thanks
0 Kudos
BrandonCales
New Contributor III
Is there supposed to be a lag when projecting the Lat/Lon, or is it based on hardware?
0 Kudos
IgressT
New Contributor II
I think the lag is because it is making a round trip to the server every time there is a map_mouse event...
0 Kudos
ChristopherHill
Occasional Contributor
Yeah, the sample posted by "vipulsoni" is using the geometry service to reproject, which is making a round trip to the server and back.
0 Kudos
LanceCrumbliss
Occasional Contributor II
I think the lag is because it is making a round trip to the server every time there is a map_mouse event...


For client side, real time conversion between a decent amount of coordinate systems, check out http://projnet.codeplex.com/

In my Silverlight app, I'm converting back and forth between state plane and geographic coordinates with it and it works perfectly.

Lance
0 Kudos