开发者

Creating function to return 2 variables?

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
        $geoco开发者_C百科de_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


$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...


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


you can return them as array();

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜