Select to view content in your preferred language

Graphic position on the map (not lat/long)

2711
7
01-07-2011 05:08 AM
SangamLama
Emerging Contributor
Hi all,

If I wanted to find out where in the map does a graphic layer lie (coordinate in the map layer, not the geographic latitude/longitude), how can I do so?
0 Kudos
7 Replies
dotMorten_esri
Esri Notable Contributor
I'm not entirely sure what you mean, but have you looked at the Map.ScreenToMap and Map.MapToScreen methods? They might do what you are looking for.
0 Kudos
SangamLama
Emerging Contributor
I'm not entirely sure what you mean, but have you looked at the Map.ScreenToMap and Map.MapToScreen methods? They might do what you are looking for.


Hi there,
based on the provided description, looks likethat's exactly what I need. But, I get X and Y as NaN respectively. Any idea why that might be happening?

here's what I've done:

            MapPoint p1 = new MapPoint();
            p1.X = (graphic.Geometry.Extent.XMax + graphic.Geometry.Extent.XMin)/2;
            p1.Y = (graphic.Geometry.Extent.YMax + graphic.Geometry.Extent.YMin)/2;

            Point p2 = MyMap.MapToScreen(p1);

(p1.X = -70 and p2.Y = 40 approximately)

And the map layer is within a canvas in SL application. Since the function appears to compute the screen coordinates relative to the upper left of the map control, I don't see why the results yield NaN....
0 Kudos
JenniferNery
Esri Regular Contributor
I'm not sure what your calculation is trying to achieve but referring to your first post, to find out the position of a graphic, you can use mouse events on the layer.

In this SDK example (Identify), MouseClick event is used and e.MapPoint gives the location using map coordinates.
http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#Identify

In this SDK example (Mouse Coords), MouseMove event is used to get both MapPoint and ScreenPoint.
http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#MouseCoords

As Morten mentioned, it is possible to convert from one coordinate system to the other by calling MapToScreen or ScreenToMap.
0 Kudos
SangamLama
Emerging Contributor
Hi Jennifer,

I'm trying to simulate a mouseleftbuttondown event on the graphic instead of actually performing it. There's a popup window tied to certain graphics on the map. When one clicks any of those graphic layers, the popup window appears with certain XY mouse offset.

But later on I ran into the need of displaying that popup window without clicking on the graphic. In order to get that offset without an actual click, I needed a function such as MapToScreen. But my implementation doesn't work. Since my question would have been too detailed the first time around, I only asked a part of it.

I'll try a couple more ways to see if it works. If anyone else has a better approach, please do tell 🙂



I'm not sure what your calculation is trying to achieve but referring to your first post, to find out the position of a graphic, you can use mouse events on the layer.

In this SDK example (Identify), MouseClick event is used and e.MapPoint gives the location using map coordinates.
http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#Identify

In this SDK example (Mouse Coords), MouseMove event is used to get both MapPoint and ScreenPoint.
http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#MouseCoords

As Morten mentioned, it is possible to convert from one coordinate system to the other by calling MapToScreen or ScreenToMap.
0 Kudos
JenniferNery
Esri Regular Contributor
How about...
MapPoint mapPoint = graphic.Extent.GetCenter(); 
Point screenPoint = MyMap.MapToScreen(mapPoint);

provided neither graphic nor its extent is null.
0 Kudos
SangamLama
Emerging Contributor
How about...
MapPoint mapPoint = graphic.Extent.GetCenter(); 
Point screenPoint = MyMap.MapToScreen(mapPoint);

provided neither graphic nor its extent is null.


Hi there,
I tried this, but I still get the value of screenPoint as NaN, NaN. Neither graphic or extent is null.
0 Kudos
JenniferNery
Esri Regular Contributor
I think the issue is when the code MapToScreen() is executed. It needs to be run when the layers are drawn to your map. Try to execute it in the LayersInitialized handler.

  public MainPage()
  {
   InitializeComponent();
   this.MyMap.Layers.LayersInitialized += Layers_LayersInitialized;
  }

  bool allLayersDrawn = false;
  void Layers_LayersInitialized(object sender, System.EventArgs args)
  {
   if (!allLayersDrawn)
   {
    GraphicsLayer l = this.MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
    if (l != null)
    {
     MapPoint mapPoint = l.Graphics[0].Geometry.Extent.GetCenter();
     Point screenPoint = this.MyMap.MapToScreen(mapPoint);
    }
    allLayersDrawn = true;
   }   
  }
0 Kudos