开发者

JSON Decode Help with PHP

I am having trouble getting some data from a JSON object which I have converted into an array:

This is my Class:

class tbfe_json_开发者_如何学JAVAapi {

    var $location;
    var $latitude;
    var $longitude;
    var $searchRadius = 20; // Default to 20 Miles
    var $reverseGeoCodeJSON;

    public function __construct() {

        $this->getLocationParams(); 

        $this->getCoords( $this->reverseGeoCodeLocation( $this->location ) );

    }

    public function getLocationParams() {

        if( isset( $_GET['location'] ) ) {  

            $this->location = str_replace(' ', '' , $_GET['location'] );

            if( isset( $_GET['radius'] ) ) {
                $this->searchRadius = $_GET['radius'];
            }   

        }
        else {
            die('Invalid parameters specified for JSON request.');
        }

    }

    public function reverseGeoCodeLocation($location) {
        $cURL = curl_init();
        curl_setopt($cURL, CURLOPT_URL, 'http://maps.google.com/maps/geo?q='.$location.'&output=json');
        curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);
        return $this->reverseGeoCodeJSON = curl_exec($cURL);
        curl_close ($cURL); 
    }

    public function getCoords($json_data) {

        $obj = json_decode($json_data, true);

        var_dump($obj);

        // I NEED THE COORDINATE VALUES HERE TO PLACE BELOW

        $this->latitude = '';
        $this->longitude = '';

    }

}

And this is the array I need to work from: http://www.thebigfishexperience.org.uk/sources/ajax/venue-json.php?location=london

I need to retrieve the coordinate values from the array and place them into my instance variables as you see above.


i believe you need:

 $this->latitude = $obj['Placemark'][0]['Point']['coordinates'][0];
 $this->longitude = $obj['Placemark'][0]['Point']['coordinates'][1];


Look at the dump of $obj and find the two values you're looking for. Then reference them to your two variables.

$this->latitude = $obj->path->to->value->one;
$this->longitude = $obj->path->to->value->two;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜