【问题标题】:Overpass API and json output in PHPPHP 中的 Overpass API 和 json 输出
【发布时间】:2017-11-03 06:39:22
【问题描述】:

我正在使用 Overpass Turbo 在地图上显示信息。 我的部分代码(我用的是PHP)如下:

//overpass query
$overpass = 'http://overpass-api.de/api/interpreter?data=[out:json];area(3600046663)->.searchArea;(node["amenity"="drinking_water"](area.searchArea););out;';
// collecting results in JSON format
$html = file_get_contents($overpass);
$jsonout = json_decode($html);
// this line just checks what the query would give as output
var_dump($jsonout);

JSON 格式的查询结果(var_dump 显示的内容)如下所示:

version:    0.6
generator:  "Overpass API"
osm3s:
timestamp_osm_base: "2017-11-03T06:25:02Z"
timestamp_areas_base:   "2017-11-03T05:45:02Z"
copyright:  "The data included in this document is from www.openstreetmap.org. The data is made available under ODbL."
elements:   
0:
type:   "node"
id: 254917402
lat:    46.0672187
lon:    11.1379545
tags:   
amenity:    "drinking_water"
1:
type:   "node"
id: 257481472
lat:    46.0687113
lon:    11.1201097
tags:   
amenity:    "drinking_water"

等等。

您可以自己查看,在浏览器中复制/粘贴查询网址:http://overpass-api.de/api/interpreter?data=[out:json];area(3600046663)-%3E.searchArea;(node[%22amenity%22=%22drinking_water%22](area.searchArea););out;

如您所见,上述数组中的每个element 都有latitudelongitude 信息。我需要那些在地图上显示标记。

我无法从数组中的每个元素中分离出latlon 信息。我该怎么做?

【问题讨论】:

    标签: php json api leaflet output


    【解决方案1】:

    给你:

    <?php
    
    // overpass query
    $overpass = 'http://overpass-api.de/api/interpreter?data=[out:json];area(3600046663)->.searchArea;(node["amenity"="drinking_water"](area.searchArea););out;';
    
    // collecting results in JSON format
    $html = file_get_contents($overpass);
    $result = json_decode($html, true); // "true" to get PHP array instead of an object
    
    // elements key contains the array of all required elements
    $data = $result['elements'];
    
    foreach($data as $key => $row) {
    
        // latitude
        $lat = $row['lat'];
    
        // longitude
        $lng = $row['lon'];
    }
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 2018-11-04
      • 2015-12-10
      • 1970-01-01
      • 1970-01-01
      • 2020-01-20
      • 2021-09-17
      • 2021-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多