这是端点 - https://www.geocode.farm/v3/json/forward/?addr=WC2H%208LG&country=uk&lang=en&count=1
我想使用php从json输出中提取lat和lon。
<?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);
?>
我正在
注意:“试图在”错误
中获取非对象的属性
$result
输出array
。
错误是从数组中获取纬度。
答案 0 :(得分:1)
我根据你的输出进行了测试。
$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);
输出
stdClass Object
(
[latitude] => 51.5153684402766
[longitude] => -0.12855210617794
)
获得lat和long
$json->geocoding_results->RESULTS[0]->COORDINATES->latitude
$json->geocoding_results->RESULTS[0]->COORDINATES->longitude
答案 1 :(得分:0)
您可能因$data = $results->COORDINATES->Latitude[0];
如果谷歌找不到您在网址中传递的地址,并且(因此未找到lat long)该对象将为空,则不会有任何属性。
因此,您将尝试使用其他正确的地址,您将不会收到此错误。