Jump to content

[SOLVED] Converting a lat/lon coordinate?


ERuiz

Recommended Posts

Hello all,

If I have a variable that stores a coordinate exactly in this format:

N18 23.5725 W66 23.8565

And I need to change this coordinate into decimal degrees such as this:

18.3928, -66.3976

In the example above, in order to get the .xxxx value, we need to divide xx.xxxx by 60 (ej: 23.5725/60 = .3928). Also, I need to have a comma separating the 2 values.

How can this be accomplished? Keep in mind that the letter W or S, needs to be converted to a - (negative sign)

The name of the variable is $position

Thanks for any help.

Regards,

Efrain Ruiz
Link to comment
https://forums.phpfreaks.com/topic/31833-solved-converting-a-latlon-coordinate/
Share on other sites

What you mean, my friend?

Essentially, I just want the final variable to be in the following format: "xx.xxxx, xx.xxxx" instead of being "Nxx xx.xxxx Wxx xx.xxxx"

Maybe you can separate the values for better manipulation, but once all converting is done, I would like to have the final variable as described above.
<?php
$degrees = explode(" ",$position);
$number = $degrees[1]/60;
if (substr($degrees[0], 0, 1) == N){$answer = $number + substr($degrees[0], 1, 4);}
if (substr($degrees[0], 0, 1) == W){$answer = 0 - 2*($number + substr($degrees[0], 1, 4);}
?>

sorry, the 1 should be 0
opps sorry, i forgot to close the if bracket...
<?php
$degrees = explode(" ",$position);
$number = $degrees[1]/60;
if (substr($degrees[0], 0, 1) == N){$answer = $number + substr($degrees[0], 1, 4);}
if (substr($degrees[0], 0, 1) == W){$answer = 0 - 2*($number + substr($degrees[0], 1, 4));}
?>
here this should work...
<?php
$degrees = explode(" ",$position);
$number = ($degrees[1]/60);
if (substr($degrees[0], 0, 1) == "N"){$answer = $number + substr($degrees[0], 1, 3);}
if (substr($degrees[0], 0, 1) == "W"){$answer = 0 - ($number + substr($degrees[0], 1, 3));}
echo "$answer";
?>

MODIFIED, PLEASE RECHECK AGAIN...
Ok, it works but partially.

In the current scenario, $Position has this value "N40 38.4541 W73 47.0027"

$answer is giving this result "40.6409016667"

As you can see, it is doing the correct conversion but it's missing the other coordinate. So far so good my friend! ;-) You are almost there!
This is the last post I am making...
[code]
<?php
$position = "W66 23.8565";//here we give a dummy variable to see if it works or not, and all your other inputs should be in this format...
$degrees = explode(" ",$position);
$number = ($degrees[1]/60);
if (substr($degrees[0], 0, 1) == "N"){$answer = $number + substr($degrees[0], 1, 3);}//if is N, we leave the number
if (substr($degrees[0], 0, 1) == "W"){$answer = 0 - ($number + substr($degrees[0], 1, 3));}//if is W, we do 0 minus the answer so we get the negative answer..
echo $answer;//here we echo the answer
?>
[/code]
hope you got through it... and i am really bad coder, kind of speeding my progress as well, so a bit rushed...
this should work no matter two or three digits of N or W...
hope you become a great mathmatician ERuiz  ;)
sorry, i didnt see your last post...
i cant separate "N40 38.4541 W73 47.0027", because there are also spaces bewteen the N40 and 38 as well as W73 and 47, if you have them like this: "N40 38.4541-W73 47.0027", it will be much easier as well as this: "N40-38.4541 W73-47.0027".

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.