[OpenLayers-Users] Wrapping vectors at the dateline??

Jani Patokallio jpatokal at iki.fi
Sun Feb 22 12:50:34 EST 2009


Greetings,

users-request at openlayers.org wrote:
> Date: Sun, 22 Feb 2009 04:42:52 -0600
> From: "David Raasch" <dr4296 at myoldhouse.com>
> Subject: [OpenLayers-Users] Wrapping vectors at the dateline??
>
> The problem I've run into is that, when my map initially loads, some points
> do not appear until you drag the map to the right a bit.  I've come to the
> conclusion that this has to do with the "dateline wrapping" issue.
> (Apparently, according to what I've read in the archives of this mailing
> list, there's no functionality available to wrap a VECTOR layer at the
> dateline??  Is that correct ??)
> Has anybody written a work-around for this issue?
>   
That's correct, and the only workaround that I know of is to render the
line again at x-360 and x+360 as applicable.  The following functions
take two decimal coordinates and return a set of Features that plot it
across the dateline

function straightPath(startPoint, endPoint) {
  // Do we cross the dateline?  If yes, then flip endPoint across it
  if(Math.abs(startPoint.x-endPoint.x) > 180) {
    if(startPoint.x < endPoint.x) {
      endPoint.x -= 360;
    } else {
      endPoint.x += 360;
    }
  }
  return [startPoint, endPoint];
}

// Draw a flight connecting (x1,y1)-(x2,y2)
function drawLine(x1, y1, x2, y2) {
  var cList = null, wList = null, eList = null;

  cList = straightPath(new OpenLayers.Geometry.Point(x1, y1), new OpenLayers.Geometry.Point(x2, y2));
  // Path is in or extends into east (+) half, so we have to make a -360 copy
  if(x1 > 0 || x2 > 0) {
    wList = straightPath(new OpenLayers.Geometry.Point(x1-360, y1), new OpenLayers.Geometry.Point(x2-360, y2));
  }
  // Path is in or extends into west (-) half, so we have to make a +360 copy
  if(x1 < 0 || x2 < 0) {
    eList = straightPath(new OpenLayers.Geometry.Point(x1+360, y1), new OpenLayers.Geometry.Point(x2+360, y2));
  }
  var features = [ new OpenLayers.Feature.Vector(new OpenLayers.Geometry.LineString(cList) ];
  if(wList) {
    features.push(new OpenLayers.Feature.Vector(new OpenLayers.Geometry.LineString(wList));
  }
  if(eList) {
    features.push(new OpenLayers.Feature.Vector(new OpenLayers.Geometry.LineString(eList));
  }
  return features;
}


Simplified from
http://openflights.svn.sourceforge.net/viewvc/openflights/openflights/openflights.js?view=markup
-- see that for a version that can also handle drawing great circle
paths, or http://openflights.org for a live demo.  And yes, I agree that
this is a real pain: for one thing, it effectively doubles the time
needed to render any vectors.

Cheers,
-jani




More information about the Users mailing list