问题描述
我有laravel输出以下内容: [
{
id:3,
lat:38.8978378,
lon:-77.0365123
},
{
id:4,
lat:44.8,
lon:1.7
},
{
id:22,
lat:37.59046 ,
lon:-122.348994
}
]
我希望它是geoJson格式:
{type:FeatureCollection,
特征:[
{type:Feature,
geometry:{type:Point,coordinates:[lat,lon]},
属性:{
name:value
}
}
]
}
我知道我需要某种循环。但我不知道如何在PHP中构造它。任何指导将不胜感激。试图构建一个地图应用程序,该应用程序可以在世界范围内显示数千个标记。我已经在考虑集群,但需要经过这个基本步骤。
谢谢!
我修改了循环,排列有点不合适。如果有人感兴趣的话,把它变成一个函数:
$ p $ 函数geoJson($ locales)
{
$ original_data = json_decode($ locales,true);
$ features = array();
$ b foreach($ original_data as $ key => $ value){
$ features [] = array(
'type'=>'Feature',
'geometry'=> array('type'=>'Point','coordinates'=> array((float)$ value ['lat'],(float)$ value ['lon'])) ,
'properties'=> array('name'=> $ value ['name'],'id'=> $ value ['id']),
);
};
$ allfeatures = array('type'=&'; FeatureCollection','features'=> $ features);
返回json_encode($ allfeatures,JSON_PRETTY_PRINT);
}
I have laravel outputting the following:
[
{
"id": 3,
"lat": "38.8978378",
"lon": "-77.0365123"
},
{
"id": 4,
"lat": "44.8",
"lon": "1.7"
},
{
"id": 22,
"lat": "37.59046",
"lon": "-122.348994"
}
]
i would like it to be the geoJson format:
{ "type": "FeatureCollection",
"features": [
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [lat, lon]},
"properties": {
"name": "value"
}
}
]
}
I know i need some sort of loop. But i'm not sure how to structure it in PHP. Any guidance would be greatly appreciated. Trying to build a map application that could have several thousand markers on a world view. I'm already thinking about clustering, but need to get past this basic step.
Thanks!
I modified the loop, the arrangement was a bit off. And turned it into a function if anyone is interested:
function geoJson ($locales)
{
$original_data = json_decode($locales, true);
$features = array();
foreach($original_data as $key => $value) {
$features[] = array(
'type' => 'Feature',
'geometry' => array('type' => 'Point', 'coordinates' => array((float)$value['lat'],(float)$value['lon'])),
'properties' => array('name' => $value['name'], 'id' => $value['id']),
);
};
$allfeatures = array('type' => 'FeatureCollection', 'features' => $features);
return json_encode($allfeatures, JSON_PRETTY_PRINT);
}
这篇关于重新格式化Json到PHP中的geoJson(Laravel)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!