Jump to content

Swap Longitude and Latitude in a string


Go to solution Solved by cyberRobot,

Recommended Posts

I have a string I am pulling out of a kml file to be used in google's static map image.  The problem is that the longitude and latitude are in the wrong order.

$xml=simplexml_load_file($kml);
//print_r($xml);
$coordinates= $xml->Document->Placemark->Polygon->outerBoundaryIs->LinearRing->coordinates;
$coordinates = str_replace(",0","|",$coordinates);
echo $coordinates;

$coordinates returns

 

-93.93740385636626,32.5298698657008| -93.94147866742821,32.52989552832681| -93.94144945527509,32.53330102162547| -93.94975589137293,32.53335196474311| -93.94983123242544,32.52989370087431| -93.94493319136991,32.52989645540433| -93.9443121915328,32.52861950574362| -93.94419758821717,32.52736388060429| -93.94302907878635,32.52652644904565| -93.94229993303671,32.5257441308524| -93.94147580320451,32.52521094897828| -93.94137978713827,32.52261795379491| -93.93742258613195,32.5225916055261| -93.93740385636626,32.5298698657008|

 

I need them as they are above separated by pipes to be used as a parameter in a url only I need the longitude and latitude swapped.

 

So instead of "-93.93740385636626,32.5298698657008", I need it to read "32.5298698657008,-93.93740385636626".  All of the coordinates I will be using in the future will be in the same general area as the locations above.

 

Link to comment
https://forums.phpfreaks.com/topic/290187-swap-longitude-and-latitude-in-a-string/
Share on other sites

  • Solution

Here's a quick script for swapping the values:

<?php
//INITIALIZE VARIABLES
$values          = "-93.93740385636626,32.5298698657008| -93.94147866742821,32.52989552832681| -93.94144945527509,32.53330102162547| -93.94975589137293,32.53335196474311| -93.94983123242544,32.52989370087431| -93.94493319136991,32.52989645540433| -93.9443121915328,32.52861950574362| -93.94419758821717,32.52736388060429| -93.94302907878635,32.52652644904565| -93.94229993303671,32.5257441308524| -93.94147580320451,32.52521094897828| -93.94137978713827,32.52261795379491| -93.93742258613195,32.5225916055261| -93.93740385636626,32.5298698657008|";
$coords          = explode('|', $values);
$correctedCoords = array();
 
//LOOP THROUGH THE COORDINATES
foreach($coords as $currLatLong) {
     //IF THE CURRENT ENTRY CONTAINS A VALUE, SWAP THE VALUES
     $currLatLong = trim($currLatLong);
     if($currLatLong != '') {
          list($val1, $val2) = explode(',', $currLatLong);
          $correctedCoords[] = "$val2,$val1";
     }
}
 
//RE-BUILD THE STRING
$correctedCoords = implode('| ', $correctedCoords);
 
//OUTPUT RESULTS
print '<pre>' . print_r($values, true) . '</pre>';
print '<pre>' . print_r($correctedCoords, true) . '</pre>';
?>
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.