[OpenLayers-Users] Two displayProjections on a map

Richard Greenwood richard.greenwood at gmail.com
Thu Jan 10 05:46:16 PST 2013


On Thu, Jan 10, 2013 at 6:02 AM, Jan Hartmann <j.l.h.hartmann at uva.nl> wrote:

> **
> Hi Rich, how did you get the lon-lat values on your map formattted as
> degrees/minutes/seconds (next to the default decimal degrees?)
>

Hey Jan -

The hardest part was getting the coordinates to be the same width so that
they did not cause other page elements to move. Without that the display
was quite "jittery". My code is below. I'm using my original CSCS
transformation library, not proj4js, but that shouldn't matter.

Best regards,
Rich

rwg.cursorCoords = function(e) {

  var position = this.events.getMousePosition(e);
  var pointXY = this.getLonLatFromPixel(position); /* this has a transform
method */
  var p = new CSCS.PT(pointXY.lon, pointXY.lat);
  var text;

  /* *** Private functions ***************** */
  var zeroPad2 = function(num) {
    num += '';
    return (num.length<2) ? "0"+num : num;
  };

  var zeroPad4 = function(num) {
    num += '';
    return (num.length<4) ? "0"+num : num;
  };

  var dd2dms = function(v) {
    var fv = v % 1;     // fractional part
    var d = v - fv;     // integer part (degrees)
    fv = Math.abs(fv);
    var m = fv * 60;    // minutes
    var mf = m % 1;     // fractional part of minutes
    var mi = m - mf;    // integer part of minutes
    var s = Math.round(mf*600)/10;  // seconds
    s = s.toFixed(1);
    return  d + "° " + zeroPad2(mi) + "' "+ zeroPad4(s) +
""";
  };

  // display original state plane coords
  text = "N: " + Math.round(p.y) + " E: " + Math.round(p.x);
  OpenLayers.Util.getElement("coords").innerHTML = text;

  // convert to long/lat and display
  CSCS.transform(csSP, csLL, p);

  text = "<table cellpadding=\"0\" cellspacing=\"0\"><tr><td>";
  text += "Long: " + dd2dms(p.x) + "</td><td> Lat: " +
dd2dms(p.y);
  text += "</td></tr><tr><td>Long: " + Math.round(p.x*10000)/10000 +
"°</td><td> Lat: " + Math.round(p.y*10000)/10000;
  text += "°</td></tr></table>";

  OpenLayers.Util.getElement("coordsLL").innerHTML = text;

  // convert long/lat to UTM and display
  CSCS.transform(csLL, csUTM, p);
  text = "X =" + Math.round(p.x) + " Y = " + Math.round(p.y);
  OpenLayers.Util.getElement("coordsUTM").innerHTML = text;

};





-- 
Richard Greenwood
richard.greenwood at gmail.com
www.greenwoodmap.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/openlayers-users/attachments/20130110/8ffbae35/attachment.html>


More information about the Users mailing list