Cody Raspien
Cody Raspien

Reputation: 1835

Getting lat lon from geocodefarm using php

This is the endpoint - https://www.geocode.farm/v3/json/forward/?addr=WC2H%208LG&country=uk&lang=en&count=1

I want extract lat and lon from the json output using php.

<?php
$url = "https://www.geocode.farm/v3/json/forward/?addr=WC2H%208LG&country=uk&lang=en&count=1";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 4);
$json = curl_exec($ch);
if(!$json) {
    echo curl_error($ch);
}
curl_close($ch);
$data = json_decode($json,true);
$results = $data['geocoding_results']['RESULTS'];
$data = $results->COORDINATES->Latitude[0];
print_r ($data);
?>

I am getting a

Notice: "Trying to get property of non-object in" error

$result outputs the array. The error is at getting the latitude off the array.

Upvotes: 0

Views: 37

Answers (2)

Himanshu Upadhyay
Himanshu Upadhyay

Reputation: 6565

You are getting that error probably due to $data = $results->COORDINATES->Latitude[0];

That will not have any property when the address you have passed in url is not found by google and (and so lat long are not found) that object will be empty.

So you will try with another proper address, you will not get this error.

Upvotes: 0

urfusion
urfusion

Reputation: 5501

I have test according to your output.

$json = 

'    { "geocoding_results": { "LEGAL_COPYRIGHT": { "copyright_notice": "Copyright (c) 2017 Geocode.Farm - All Rights Reserved.", "copyright_logo": "https:\/\/www.geocode.farm\/images\/logo.png", "terms_of_service": "https:\/\/www.geocode.farm\/policies\/terms-of-service\/", "privacy_policy": "https:\/\/www.geocode.farm\/policies\/privacy-policy\/" }, "STATUS": { "access": "FREE_USER, ACCESS_GRANTED", "status": "SUCCESS", "address_provided": "WC2H 8LG", "result_count": 1 }, "ACCOUNT": { "ip_address": "217.70.180.226", "distribution_license": "NONE, UNLICENSED", "usage_limit": "250", "used_today": "9", "used_total": "9", "first_used": "06 Jul 2017" }, "RESULTS": [ { "result_number": 1, "formatted_address": "London WC2H 8LG, UK", "accuracy": "UNKNOWN_ACCURACY", "ADDRESS": { "admin_2": "Greater London", "admin_1": "England", "postal_code": "WC2H 8LG", "country": "United Kingdom" }, "LOCATION_DETAILS": { "elevation": "UNAVAILABLE", "timezone_long": "UNAVAILABLE", "timezone_short": "Europe\/London" }, "COORDINATES": { "latitude": "51.5153684402766", "longitude": "-0.12855210617794" }, "BOUNDARIES": { "northeast_latitude": "51.5153684402766", "northeast_longitude": "-0.12855210617794", "southwest_latitude": "51.5140174197844", "southwest_longitude": "-0.12981378029212" } } ], "STATISTICS": { "https_ssl": "ENABLED, SECURE" } } }';

$json = json_decode($json);
echo "<pre>";
print_r($json->geocoding_results->RESULTS[0]->COORDINATES);

Output

stdClass Object
(
    [latitude] => 51.5153684402766
    [longitude] => -0.12855210617794
)

For getting lat and long

$json->geocoding_results->RESULTS[0]->COORDINATES->latitude
$json->geocoding_results->RESULTS[0]->COORDINATES->longitude

Upvotes: 1

Related Questions