JSON数组 - 提取lat lng

时间:2021-12-29 21:13:33

I have a function that is trying to get the coordinates from a json array. It has me stumped so I was hoping someone would see my error.

我有一个试图从json数组获取坐标的函数。它让我难过,所以我希望有人会看到我的错误。

function getCoords($location){
        $loc = urlencode($location);
        $url = "https://maps.googleapis.com/maps/api/geocode/json?address=$loc&sensor=false";
        //echo $url;
        //echo file_get_contents($geo_url);
        $json_result = json_decode(file_get_contents($url));

      $geo_result =  $json_result->results[0];
        $aCsv = $geo_result->geometry->location;


        //see if the it is locating the real address or just apox location, or in most cases a bad address
        if($aCsv->location[0] == 200) {
        return array('lat'=>(float)$aCsv[2], 'lng'=>(float)$aCsv[3], 'prec'=>(int)$aCsv[1]);
         }

        var_dump($aCsv);
    }

The var dump is below I need to extract just the lat and lng in this format 42.3584308,-71.0597732, 21.3069444, -157.8583333

var转储低于我需要以这种格式提取lat和lng 42.3584308,-71.0597732,21.3069444,-157.8583333

object(stdClass)#12 (2) { ["lat"]=> float(42.3584308) ["lng"]=> float(-71.0597732) } object(stdClass)#6 (2) { ["lat"]=> float(21.3069444) ["lng"]=> float(-157.8583333) } 

I think this part is where I am running into my issue

我认为这部分是我遇到问题的地方

if($aCsv->location[0] == 200) {
            return array('lat'=>(float)$aCsv[2], 'lng'=>(float)$aCsv[3], 'prec'=>(int)$aCsv[1]);

TIA steve

Guys is there a way to access the JSON data outside of the function? I would like to take the actual address that is being parsed and show that in the result ing calculation.

伙计们有没有办法在函数外部访问JSON数据?我想获取正在解析的实际地址,并在结果计算中显示。

Sometimes google maps does not use the exact location for instance if I type in KLAX - Los Angeles airport if uses a address in Germany but if I put LAX it uses teh LA airport so it would be nice to see the parsed address as well.

例如,如果我输入KLAX - 洛杉矶机场,如果在德国使用地址,但是如果我把LAX用于洛杉矶机场,那么google地图有时不会使用确切的位置,所以很高兴看到解析的地址。

I can echo the address thats being parsed within the function but when I try to use it outside the function it is NULL. Thanks Again for your assistance. Steve

我可以回显函数中被解析的地址,但是当我尝试在函数外部使用它时,它是NULL。再次感谢你的帮助。史蒂夫

4 个解决方案

#1


0  

Trees and woods? You already have it !!

树木和树林?你已经拥有它了!!

function getCoords($location){
    $loc = urlencode($location);
    $url = "https://maps.googleapis.com/maps/api/geocode/json?address=$loc&sensor=false";
    $json_result = json_decode(file_get_contents($url));

    $geo_result =  $json_result->results[0];
    $aCsv = $geo_result->geometry->location;

    echo 'lat : '.$aCsv->lat.'<br>';
    echo 'lng : '.$aCsv->lng.'<br>';
}

getCoords('alabama');

outputs

lat : 32.3182314
lng : -86.902298

lat:32.3182314 lng:-86.902298

#2


0  

try this

$data   = json_decode($ResponseData); 

if('OK' === $data->status){
    $latitude       = $data->results['0']->geometry->location->lat;
    $longitude      = $data->results['0']->geometry->location->lng;
}

#3


0  

It's easier to work with a assoziative array so you can use this:

使用assoziative数组更容易,因此您可以使用:

$json_result = json_decode(file_get_contents($url),true) 

(http://php.net/manual/de/function.json-decode.php)

Then the values are available with:

然后这些值可用于:

$json_result["lng"] 

etc.

#4


0  

Use this

$geocode = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.$loc.'&sensor=true');              

$output = json_decode($geocode);

if($output->results)
{


   foreach($output as $data)
   {

       $lat      = $data[0]->geometry->location->lat;
       $long     = $data[0]->geometry->location->lng;
       $location = $data[0]->formatted_address;
   }
}

#1


0  

Trees and woods? You already have it !!

树木和树林?你已经拥有它了!!

function getCoords($location){
    $loc = urlencode($location);
    $url = "https://maps.googleapis.com/maps/api/geocode/json?address=$loc&sensor=false";
    $json_result = json_decode(file_get_contents($url));

    $geo_result =  $json_result->results[0];
    $aCsv = $geo_result->geometry->location;

    echo 'lat : '.$aCsv->lat.'<br>';
    echo 'lng : '.$aCsv->lng.'<br>';
}

getCoords('alabama');

outputs

lat : 32.3182314
lng : -86.902298

lat:32.3182314 lng:-86.902298

#2


0  

try this

$data   = json_decode($ResponseData); 

if('OK' === $data->status){
    $latitude       = $data->results['0']->geometry->location->lat;
    $longitude      = $data->results['0']->geometry->location->lng;
}

#3


0  

It's easier to work with a assoziative array so you can use this:

使用assoziative数组更容易,因此您可以使用:

$json_result = json_decode(file_get_contents($url),true) 

(http://php.net/manual/de/function.json-decode.php)

Then the values are available with:

然后这些值可用于:

$json_result["lng"] 

etc.

#4


0  

Use this

$geocode = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.$loc.'&sensor=true');              

$output = json_decode($geocode);

if($output->results)
{


   foreach($output as $data)
   {

       $lat      = $data[0]->geometry->location->lat;
       $long     = $data[0]->geometry->location->lng;
       $location = $data[0]->formatted_address;
   }
}