繁体   English   中英

从PHP中的geoJSON多维数组检索坐标

[英]Retrieving coordinates from geoJSON multidimentional array in PHP

我要把剩下的头发拔掉。 我需要从geoJSON数组中获取坐标和地理围栏类型(在本例中为Polygon)。 以下是我所拥有的。 提前致谢。 拉里

$str='{"type":"FeatureCollection","features":[{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[-77.0416259765625,38.89530825492018],[-77.03295707702638,38.89351294034218],[-77.03700184822084,38.89317057287496],[-77.0323669910431,38.892193563954955],[-77.04053163528442,38.89286160569515],[-77.0416259765625,38.89530825492018]]]}}]}';

$json = json_decode($str);
echo'<pre>';print_r($json);echo'</pre>';
$set = 1;
foreach($json->type->geometry->coordinates[0] as $coordinates)
{
    echo 'Set '.$set.': ';$set++;
    echo $coordinates[0].','.$coordinates[1].'<br>';
}

假设只有一个“功能”,只需将您的foreach循环更改为:

foreach($json->features[0]->geometry->coordinates[0] as $coordinates)

完整的工作代码:

<?php
$str='{"type":"FeatureCollection","features":[{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[-77.0416259765625,38.89530825492018],[-77.03295707702638,38.89351294034218],[-77.03700184822084,38.89317057287496],[-77.0323669910431,38.892193563954955],[-77.04053163528442,38.89286160569515],[-77.0416259765625,38.89530825492018]]]}}]}';

$json = json_decode($str);
// echo'<pre>';print_r($json);echo'</pre>';
$set = 1;
foreach($json->features[0]->geometry->coordinates[0] as $coordinates)
{
    echo 'Set '.$set.': ';$set++;
    echo $coordinates[0].','.$coordinates[1]."<br>\n";
}

// Output:

// Set 1: -77.041625976562,38.89530825492<br>
// Set 2: -77.032957077026,38.893512940342<br>
// Set 3: -77.037001848221,38.893170572875<br>
// Set 4: -77.032366991043,38.892193563955<br>
// Set 5: -77.040531635284,38.892861605695<br>
// Set 6: -77.041625976562,38.89530825492<br>
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM