How to calculate the pixel width of entire world (at current zoom level)?

638
2
11-22-2011 01:15 PM
RexBradford
New Contributor II
I'm not even sure the title of this post is a valid concept.

But I am trying to track point overlays on a map, in pixel coordinates for fast manipulation, and things work fine until you wrap around the world horizontally.  Then the Graphic objects visually show up again, projected properly with wraparound, but the tracking code thinks they are way far to the left or right.

Specifically, map.toScreen(graphic.geometry) for points (Graphic objects in my case) in a wrapped view do not return the same x values as they would were the view not wrapped (by wrapped, I mean I panned the map using the UI).

I.e., if you pan the map eastward and wrap around, map.toScreen() will return negative numbers for the graphic objects which have "gone left off the screen but then have now returned from the right side".  At least it does this for me.

The offset which would fix this is presumably the pixel width of the entire world at the current zoom level.

Is it possible to calculate this value?

If it matters, this is spatial reference 102100 (WGS_1984_Web_Mercator_Auxiliary_Sphere).

Thanks,

Rex Bradford
Direct Relief International
0 Kudos
2 Replies
JianHuang
Occasional Contributor III
Rex,

When you say "tracking code", what you are trying to achieve? A better understanding use case would allow us to help you more.
To answer your question. In order to get the frame width of a map in current zoom level, you can call map._getFrameWidth();
And here is the code snippet illustrating how to move the screen point to the world in which the current map extent is.

    var mapFrameWidth = map._getFrameWidth();
    dojo.forEach(featurePts, function (pt, idx) {
      var featureScreenPt = map.toScreen(pt, true);
      if (mapFrameWidth !== -1) {
        featureScreenPt.x = featureScreenPt.x % mapFrameWidth;
        if (featureScreenPt.x < 0) {
          featureScreenPt.x += mapFrameWidth;
        }
        if (map.width > mapFrameWidth) {
          var margin = (map.width - mapFrameWidth)/2;
          while (featureScreenPt.x < margin) {
            featureScreenPt.x += mapFrameWidth;
          }
        }
      }
    }
0 Kudos
RexBradford
New Contributor II
Rex,

When you say "tracking code", what you are trying to achieve? A better understanding use case would allow us to help you more.
To answer your question. In order to get the frame width of a map in current zoom level, you can call map._getFrameWidth();



Thanks, that's exactly what I was looking for.

In answer to your question, I have developed a UI system which keeps track of all graphic markers and provides a UI for selection which knows exactly which markers are under the cursor, even if they are obscured by other markers.  Rather than do this via a (round trip) geographic query, I throw all markers into a 2D Javascript sparse bucket map, and then can do a fast lookup for a given x,y cursor position just by seeing who inhabits the bucket the cursor is in (and then doing exact rectangle matches on just those markers rather than the whole list).  But my code wasn't working when you pan the wraparound map; now it does, by normalizing x-coords with map._getFrameWidth().

Rex Bradford
Direct Relief International
0 Kudos