On Thu, Jan 10, 2013 at 6:02 AM, Jan Hartmann <[email protected]> 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 + "&deg;&nbsp;" + zeroPad2(mi) + "'&nbsp;"+ zeroPad4(s) +
"&quot;";
  };

  // 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:&nbsp;" + dd2dms(p.x) + "</td><td>&nbsp;Lat:&nbsp;" +
dd2dms(p.y);
  text += "</td></tr><tr><td>Long:&nbsp;" + Math.round(p.x*10000)/10000 +
"&deg;</td><td>&nbsp;Lat:&nbsp;" + Math.round(p.y*10000)/10000;
  text += "&deg;</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
[email protected]
www.greenwoodmap.com
_______________________________________________
Users mailing list
[email protected]
http://lists.osgeo.org/mailman/listinfo/openlayers-users

Reply via email to