0

我很难从 Here Maps API(处理 JSON)获取信息。我正在尝试将 JSON 转换为字符串,以便可以将其保存在数据库中。

经纬度对我的项目来说是最重要的。

$endereco = 'Rua Canapi 193, Guarulhos, Sao Paulo, Brasil';
$endereco = str_replace(" ", "+", $endereco);
$id = 'devportal-demo-20180625';
$cod = '9v2BkviRwi9Ot26kp2IysQ';

// https://geocoder.api.here.com/6.2/geocode.json?app_id=jz23R9Wi89IqwnnxZno0&app_code=g6DikMOdkOUyvGZN3kjW5A&searchtext=425+W+Randolph+Chicago
//$json = file_get_contents("http://autocomplete.geocoder.api.here.com/6.2/suggest.json?app_id=$id&app_code=$cod&query=$endereco&gen=9");//
//$json = file_get_contents("https://geocoder.api.here.com/6.2/geocode.json?searchtext=$endereco&app_id=$id&app_code=$cod&gen=9");
$json = file_get_contents("https://geocoder.api.here.com/6.2/geocode.json?app_id=$id&app_code=$cod&searchtext=$endereco");

    echo'<pre>';
    //print_r($json = json_decode($json, true));
    print_r($json = json_decode($json));
    echo'</pre>';

//echo $json->{'suggestions'}[0]->{'label'};
//var_dump( $json->{'Response'}{'MetaInfo'}[0]->{'Timestamp'});

如您所见,我正在尝试使用文档页面中存在的不同 json url Here maps api,还有其他方法,但我想要的是具有纬度和经度的方法

4

2 回答 2

1

我在这里应用了您的示例数据。

它创建了这个呈现的 url:

https://geocoder.api.here.com/6.2/geocode.json?searchtext=Rua%20Canapi%20193%2C%20Guarulhos%2C%20Sao%20Paulo%2C%20Brasil&app_id=jz23R9Wi89IqwnnxZno0&app_code=g6DikMOdkOUyvGZN3kjW5A&

并生成了这个 json 数据:

{
  "Response": {
    "MetaInfo": {
      "Timestamp": "2018-10-17T03:35:57.353+0000"
    },
    "View": [
      {
        "_type": "SearchResultsViewType",
        "ViewId": 0,
        "Result": [
          {
            "Relevance": 1,
            "MatchLevel": "houseNumber",
            "MatchQuality": {
              "Country": 1,
              "State": 1,
              "City": 1,
              "Street": [
                1
              ],
              "HouseNumber": 1
            },
            "MatchType": "pointAddress",
            "Location": {
              "LocationId": "NT_ySMW7zgrnTcVMzktS0t7FC_xkzM",
              "LocationType": "point",
              "DisplayPosition": {
                "Latitude": -23.46615,
                "Longitude": -46.42356
              },
              "NavigationPosition": [
                {
                  "Latitude": -23.4662,
                  "Longitude": -46.42354
                }
              ],
              "MapView": {
                "TopLeft": {
                  "Latitude": -23.4650258,
                  "Longitude": -46.4247855
                },
                "BottomRight": {
                  "Latitude": -23.4672742,
                  "Longitude": -46.4223345
                }
              },
              "Address": {
                "Label": "Rua Canapi, 193, Pimentas, Guarulhos - SP, 07272-060, Brasil",
                "Country": "BRA",
                "State": "SP",
                "City": "Guarulhos",
                "District": "Pimentas",
                "Street": "Rua Canapi",
                "HouseNumber": "193",
                "PostalCode": "07272-060",
                "AdditionalData": [
                  {
                    "value": "Brasil",
                    "key": "CountryName"
                  },
                  {
                    "value": "São Paulo",
                    "key": "StateName"
                  }
                ]
              }
            }
          }
        ]
      }
    ]
  }
}

首先将 json 解码为对象或数组。对象语法没有任何问题,所以我将展示这一点。

$obj = json_decode($json);

Timestamp使用对象语法隔离值->,不需要花括号和引号,它可以如下所示:(Demo

echo $obj->Response->MetaInfo->Timestamp;
// 2018-10-17T03:35:57.353+0000

至于这是什么:$json->{'suggestions'}[0]->{'label'};该数据不可用。

对于纬度和经度数据,这是完整的电池:

代码:(演示

echo "Display Position Latitude: " , $obj->Response->View[0]->Result[0]->Location->DisplayPosition->Latitude;
echo "\nDisplay Position Longitude: " , $obj->Response->View[0]->Result[0]->Location->DisplayPosition->Longitude;
echo "\nNavigation Position Latitude: " , $obj->Response->View[0]->Result[0]->Location->NavigationPosition[0]->Latitude;
echo "\nNavigation Position Longitude: " , $obj->Response->View[0]->Result[0]->Location->NavigationPosition[0]->Longitude;
echo "\nMap View Top Left Latitude: " , $obj->Response->View[0]->Result[0]->Location->MapView->TopLeft->Latitude;
echo "\nMap View Top Left Longitude: " , $obj->Response->View[0]->Result[0]->Location->MapView->TopLeft->Longitude;
echo "\nMap View Bottom Right Latitude: " , $obj->Response->View[0]->Result[0]->Location->MapView->BottomRight->Latitude;
echo "\nMap View Bottom Right Longitude: " , $obj->Response->View[0]->Result[0]->Location->MapView->BottomRight->Longitude;

输出:

Display Position Latitude: -23.46615
Display Position Longitude: -46.42356
Navigation Position Latitude: -23.4662
Navigation Position Longitude: -46.42354
Map View Top Left Latitude: -23.4650258
Map View Top Left Longitude: -46.4247855
Map View Bottom Right Latitude: -23.4672742
Map View Bottom Right Longitude: -46.4223345

最后,为了使您的查询字符串构建任务更简单/更干净,请使用http_build_query()

代码:(演示

$data = [
    'searchtext' => 'Rua Canapi 193, Guarulhos, Sao Paulo, Brasil',
    'app_id' => 'devportal-demo-20180625',
    'app_code' => '9v2BkviRwi9Ot26kp2IysQ'
];

$json = file_get_contents('https://geocoder.api.here.com/6.2/geocode.json?' . http_build_query($data));
于 2018-10-17T03:50:20.517 回答
0

您正在以正常方式使用json_decode()方法。它会返回一个OBJECT。要执行您想要的操作,您需要将true作为第二个参数传递给方法,将结果作为ARRAY返回。

此外,不要在 URI 参数处执行str_replace() ,使用urlencode()方法,它将处理所有特殊字符。

您修改后的代码将是:

<?php 
$endereco = "Rua Canapi 193, Guarulhos, Sao Paulo, Brasil";
$id = "here_id";
$cod = "here_cod";

$json = json_decode ( file_get_contents ( "https://geocoder.api.here.com/6.2/geocode.json?app_id=" . urlencode ( $id) . "&app_code=" . urlencode ( $cod) . "&searchtext=" . urlencode ( $endereco)), true);

echo "Longitude: " . $json["Response"]["View"][0]["Result"][0]["Location"]["DisplayPosition"]["Longitude"] . "<br />";
echo "Latitude: " . $json["Response"]["View"][0]["Result"][0]["Location"]["DisplayPosition"]["Latitude"] . "<br />";

echo "<pre>";
print_r ( $json);
echo "</pre>";
?>
于 2018-10-17T03:21:44.117 回答