SHOW ON MAP MOUSE HOVER - LAT/LONG VALUES

5312
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
ChrisBradberry
Occasional Contributor
I used a timer and had it check the mouse move every 5 seconds.  Not perfect, but it keeps the lat long display from cycling after the mouse has stopped moving.  Here is the code:

        #region Mouse Coordinates on screen
        //get the coordinates to show on the menu bar

        System.Windows.Threading.DispatcherTimer mouseTimer = new System.Windows.Threading.DispatcherTimer();
        ESRI.ArcGIS.Client.Geometry.MapPoint mapPoint; //initiate a map point to get the screen map point

        private void ArcGISTiledMapServiceLayer_Initialized(object sender, EventArgs e)
        {
            //Start the timer 
            mouseTimer.Interval = new TimeSpan(0, 0, 0, 0, 500); // 500 Milliseconds = 1/2 second
            mouseTimer.Tick += new EventHandler(mouseTimer_Tick);

            mouseTimer.Start();

            Map.MouseMove += new System.Windows.Input.MouseEventHandler(MyMap_MouseMove);

        }

        void mouseTimer_Tick(object sender, EventArgs e)
        {
            double mouseX;
            double mouseY;

            if (mapPoint != null)
            {
                Graphic mouseGraphic = new Graphic();

                mouseX = mapPoint.X;
                mouseY = mapPoint.Y;
                mouseGraphic.Geometry = mapPoint;

                  GeometryService latlonService = new GeometryService("http://<server>/ArcGIS/rest/services/Geometry/GeometryServer");
                latlonService.ProjectCompleted += new EventHandler<GraphicsEventArgs>(latlonService_ProjectCompleted);
                latlonService.Failed += new EventHandler<TaskFailedEventArgs>(latlonService_Failed);
                SpatialReference sr = new SpatialReference();
                sr.WKID = 4326;
                IList<Graphic> inG = new List<Graphic>();
                inG.Add(mouseGraphic);

                latlonService.ProjectAsync(inG, sr);
            }
        }


        private void MyMap_MouseMove(object sender, System.Windows.Input.MouseEventArgs args)
        {
            if (Map.Extent != null)
            {
                Graphic mouseGraphic = new Graphic();

                System.Windows.Point screenPoint = args.GetPosition(Map);
                mapPoint = Map.ScreenToMap(screenPoint);
            }
        }

        void latlonService_Failed(object sender, TaskFailedEventArgs e)
        {
            MessageBox.Show(e.Error.Message + "Latitude Longitude Service Failed");
        }

        void latlonService_ProjectCompleted(object sender, GraphicsEventArgs e)
        {
            IList<Graphic> results = e.Results;
            string mouseCoords = "Latitude: " + Utilities.DDtoDMS(results[0].Geometry.Extent.YMax, false);
            mouseCoords += ", Longitude: " + Utilities.DDtoDMS(results[0].Geometry.Extent.XMax, false);
            LatLongText.Text = mouseCoords;

        }
        #endregion
0 Kudos
DeeptiVB
New Contributor II

Hope this helps someone. Was not able to get it to work by using projections. Found an old archived forum thread which helped me convert to lat long mathematically. Here's the code below.

private void MyMap_MouseMove(object sender, System.Windows.Input.MouseEventArgs args)
{
     if (MyMap.Extent != null)
     {
         System.Windows.Point screenPoint = args.GetPosition(MyMap);
         ESRI.ArcGIS.Client.Geometry.MapPoint mapPoint = MyMap.ScreenToMap(screenPoint);
         if (MyMap.WrapAroundIsActive)
             mapPoint = ESRI.ArcGIS.Client.Geometry.Geometry.NormalizeCentralMeridian(mapPoint) as ESRI.ArcGIS.Client.Geometry.MapPoint;

         Double longi = mapPoint.X;
         Double lati = mapPoint.Y;
         Double pi = 3.14159267;
         Double magicnum = 6356752.3142;
         Double radtodeg = 57.295779513082322;

         Double latitude = new Double();
         Double longitude = new double();

         latitude = radtodeg * ((2 * Math.Atan(Math.Exp(lati / magicnum))) - (pi / 2));
         longitude = radtodeg * (longi / magicnum);

         StatusTextBlock.Text = string.Format("Map Coords: X = {0}, Y = {1}",
             Math.Round(latitude, 4), Math.Round(longitude, 4));

     }
}
0 Kudos