Return to Project-GC

Welcome to Project-GC Q&A. Ask questions and get answers from other Project-GC users.

If you get a good answer, click the checkbox on the left to select it as the best answer.

Upvote answers or questions that have helped you.

If you don't get clear answers, edit your question to make it clearer.

0 votes
448 views
I would like to print the latitude/longitude. PGC_GetFinds retrieves decimal format. I would like to print, say, N 33° 34.223' W 123° 23.112'

Any chance? Would recalculation be accurate enough to show coords like the default in GC listings? Maybe there is a chance to get lat/lon by calling PGC_GetFinds already?

 

Thx
Hampf
in Miscellaneous by hampf (4.3k points)

1 Answer

0 votes
 
Best answer

Recalculating it is good enough, at least I don't know any difference between Geocaching.com and Project-GC.com. Project-GC always stores in fixed decimal values, and I assume Geocaching.com does the same. At least that's how the data is in the API.

The following code is PHP, not very beautiful, but it works (and this editor screwed the indentation as well). I am sure you will end up with better looking code. :)

function GPS_D2DM($data) // This one loses track of negative/positive
{
$D = intval($data);
$M = round(abs(($data-$D)*60), 3);
 
return array($D, $M);
}
 
function DD_DD2Coords($lat, $lon)
{
list($lat, $lon) = DD_DD2Coords2($lat, $lon);
return "{$lat} {$lon}";
}
 
function DD_DD2Coords2($lat, $lon)
{
list($latD, $latM) = GPS_D2DM($lat);
list($lonD, $lonM) = GPS_D2DM($lon);
 
if($lat > 0)
$ns = 'N';
else
$ns = 'S';
if($lon > 0)
$ew = 'E';
else
$ew = 'W';
 
$latD = abs($latD);
$lonD = abs($lonD);
 
 
 
$lat = sprintf("%s %02d° %02s.%03s", $ns, $latD, floor($latM), round(($latM-floor($latM))*1000));
$lon = sprintf("%s %02d° %02s.%03s", $ew, $lonD, floor($lonM), round(($lonM-floor($lonM))*1000));
 
return array($lat, $lon);
}
 
by magma1447 (Admin) (242k points)
selected by hampf
Wow, thanks for the quick response. Now two more Palindrome challenge checkers are ready.
@ganja:
My lua version of the PHP code:

function GPS_D2DM(data)
-- due to lacking 'int' datatype (MathFloor() doesn't work for negative 'float's)
  local raw=ToNumber(data)
  local dAbs=MathAbs(raw)
  local d = MathFloor(dAbs)
  local m = ToNumber(StringFormat("%.03f", (dAbs-d)*60))
  if raw ~= 0 then d=dAbs/raw*d end
  return d, m
end

function DD_DD2Coords(lat, lon)
  lat, lon = DD_DD2Coords2(lat, lon)
  return lat.." "..lon
end

function DD_DD2Coords2(lat, lon, strict)
  local ns, ew
  local latD, latM = GPS_D2DM(lat)
  local lonD, lonM = GPS_D2DM(lon)
  if ToNumber(lat) > 0 then
    ns = 'N'
  else
    ns = 'S'
  end
  if ToNumber(lon) > 0 then
    ew = 'E'
  else
    ew = 'W'
  end
  
  latD = MathAbs(latD)
  lonD = MathAbs(lonD)
  if strict then
    lat = StringFormat("%s %02d° %02d.%03d", ns, latD, MathFloor(latM), MathFloor((latM - MathFloor(latM))*1000 + 0.5));
    lon = StringFormat("%s %02d° %02d.%03d", ew, lonD, MathFloor(lonM), MathFloor((lonM - MathFloor(lonM))*1000 + 0.5));
  else
    lat = StringFormat("%s %d° %d.%03d", ns, latD, MathFloor(latM), MathFloor((latM - MathFloor(latM))*1000 + 0.5));
    lon = StringFormat("%s %d° %d.%03d", ew, lonD, MathFloor(lonM), MathFloor((lonM - MathFloor(lonM))*1000 + 0.5));
  end
  return lat, lon
end
...