0

Here is my function:

<?php
function latLng($str){
    $address = $str;
    // Initialize delay in geocode speed
    $delay = 0;
    $base_url = "http://maps.google.com/maps/geo?output=xml&key=" . $key;

    // Iterate through the rows, geocoding each address
    $geocode_pending = true;
    $request_url = $base_url . "&q=" . urlencode($address);
    $xml = simplexml_load_file($request_url) or die("url not loading");

    $status = $xml->Response->Status->code;
    if (strcmp($status, "200") == 0) {
        // Successful geocode
        $geocode_pending = false;
        $coordinates = $xml->Response->Placemark->Point->coordinates;
        $coordinatesSplit = split(",", $coordinates);
        // Format: Longitude, Latitude, Altitude
        $lat = $coordinatesSplit[1];
        $lng = $coordinatesSplit[0];
    } else if (strcmp($status, "620") == 0) {
        // sent geocodes too fast
        $delay += 100000;
    } else {
        // failure to geocode
        $geocode_pending = false;
        echo "Address " . $address . " failed to geocoded. ";
        echo "Received status " . $status . "\n";
        usleep($delay);
    }
    echo $lat . "<br />";
    echo $lng;
}

echo latLng("Praha City Center, Klimentska 46, Prague, Czech Republic, 11002");
?>

It echos the lat and lng on page perfectly, but I want to return them as variables, so basically I wrap an address in the function and then a $lat and a $lng variable are returned to the page for me to use.

How do I go about doing this?

Thank you

4 Answers 4

2
$returnArray['latitude'] = $lat;
$returnArray['longitude'] = $lng;

return $returnArray;

Or better yet, make an object called EarthCoordinate that has a lat and long, and set them to that. you can then make methods to find distances between lat/long etc...

Sign up to request clarification or add additional context in comments.

1 Comment

'...find distances between lat/long etc...' - you make it sound sooo easy!
1

Two options:

  1. Return $coordinatesSplit array instead and print its elements when required
  2. Make two by-ref parameters to the function:

    function latLng($str, &$lat, &$lng) {
      //.....
       $lat = $coordinatesSplit[1];
       $lng = $coordinatesSplit[0];
      // ....
    }
    

then use them:

     $lat = $lng = null;
     latLng("Praha City Center, Klimentska 46, Prague, Czech Republic, 11002", $lat, $lng);
     echo $lat;
     echo $lng; // or whatever you want to do with them

Comments

1

you can return them as array();

return array('lat' => $lat, 'lng' => $lng);

Comments

0

You can use an array or just combine both and return them:

<?php
   function latLng($str){
     .......
     .......
     geo_data = array()
     geo_data['lat'] = $lat;
     geo_data['lng'] = $lng;

     return $geo_data;
   }

   $geo_data = latLng("Praha City Center, Klimentska 46, Prague, Czech Republic, 11002");
   var_dump($geo_data);

?>

that should help

//EDIT Oh wait - you already use an array.

just use

return $coordinatesSplit;

and call the function:

$geo_data = latLng("Praha City Center, Klimentska 46, Prague, Czech Republic, 11002"); 
var_dump($geo_data);

$geo_data[1] = lat $geo_data[0] = lng

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.