gpt4 book ai didi

javascript - 带传单 js 的动态 PHP geo json 示例

转载 作者:行者123 更新时间:2023-12-04 01:21:28 25 4
gpt4 key购买 nike

大家好。 Here是一些静态的 GeoJson 的传单主站点上的示例。我想通过将密度节点数据换成来自数据库 (DB) 的数据来使其更具动态性。下面是一些代码,展示了我尝试将静态代码与从 PHP 数据库中检索到的数据混合在一起的尝试。

$jsonStr = "{\"type\":\"FeatureCollection\",\"features\":[";
foreach($data as $info){
if($counter == $count-1){
$jsonStr .= "
{\"type\":\"Feature\",\"id\":\"".$info["site_state"]."\",\"properties\":
{
\"name\":\"".$info["state_dscr"]."\",\"density\":".$info["coalesce"]."
}
}";
}else{
$jsonStr .= "
{\"type\":\"Feature\",\"id\":\"".$info["site_state"]."\",\"properties\":
{
\"name\":\"".$info["state_dscr"]."\",\"density\":".$info["coalesce"]."
}
},";
}
$counter++;
}

$jsonStr .= "]}";

$test = json_decode($jsonStr);
//Validate the JSON
if($test === NULL){
error_log("There is an issue with your json.");
}else{
header('Content-Type: application/json');
echo $jsonStr;
}

上面的代码是通过 D3 json() 调用运行的,如下所示:

d3.json("js/statesDataJson.php", function(vioData) { ...}

在另一个js文件中。是的,我的 json 缺少状态坐标,这就是为什么我无法重现传单教程网站示例中的等值线层(见下面的链接)。

这是我试图让这个例子被发现 here更有活力。我想我可以混合来自两个来源的数据并使用来自一个来源的“desnity”节点数据并使用来自另一个来源的“geometry”节点来构建状态层。这就是我遇到问题的地方。
我最初的想法是在数据库中为地理 JSON 设置一个表,然后继续我上面动态复制 this 的示例。 .

我想知道我是否正在使用炮弹杀死一只蟑螂,是否有更有效的策略或解决方案。还是数据库是动态重建我的 json 的唯一方法?例如,是否有人会尝试解析 json 文件并编辑密度节点?似乎... asinine 但我愿意接受建议。

谢谢

最佳答案

有一种在 PHP 中生成和使用 (Geo)JSON 的更简单、更简洁的方法。正如 Bibhas 指出的那样,连接字符串非常麻烦且容易出错。在 PHP 中构建一个对象,然后将其编码为 JSON 非常容易。示例:

//php array with data
$phpArray = array(
array('id' => 'Foo', 'data' => 500),
array('id' => 'Bar', 'data' => 1000)
);

//jsonString containing geometries
$jsonString = '{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[0,0]},"id":"Foo","properties":{"name":"Foo description"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[0,0]},"id":"Bar","properties":{"name":"Bar description"}}]}';

//decode jsonString to jsonObject
$jsonObject = json_decode($jsonString);

//loop through jsonObject
foreach($jsonObject->features as $key => $feature) {
//check if there is data for this feature
foreach($phpArray as $row) {
if ($feature->id == $row['id']) {
//attach data to feature
$feature->properties->data = $row['data'];
}
}
//overwrite feature in jsonObject
$jsonObject->features[$key] = $feature;
}

//encode and output jsonObject
header('Content-Type: application/json');
echo json_encode($jsonObject);

输出:

{
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [0, 0]
},
"id": "Foo",
"properties": {
"name": "Foo description",
"data": 500
}
},{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [0, 0]
},
"id": "Bar",
"properties": {
"name": "Bar description",
"data": 1000
}
}]
}

既然您正在使用 PHP,那么最简单的事情就是使用 PHP 对象。那些你可以轻松编辑、比较、合并,无论你想做什么。

关于javascript - 带传单 js 的动态 PHP geo json 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20912652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com