我把头发拉出来了。我需要从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>';
}
答案 0 :(得分:0)
假设只有一个“功能”,只需将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>
?>