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
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.
Link to comment
Share on other sites

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));}
?>
Link to comment
Share on other sites

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...
Link to comment
Share on other sites

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!
Link to comment
Share on other sites

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  ;)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.