duancong2965
2018-11-19 18:02
浏览 118
已采纳

如何在json中为叶子实时制作正确的数组

I am trying us to leaflet.js realtime. I have it working when I encode the coordinates like below. I have read the instructions and it does not specify how it wants the data but found this in a sample. How do I encode it to print it out as it should and is it possible to include more than one set of coordinates?

Any help would be great, thanks.

my php

$lat = "52.45238209999999";
$lng = "-1.743507099999988";

$str = "{".chr(34)."geometry".chr(34).": {".chr(34)."type".chr(34).": ".chr(34)."Point".chr(34).", ".chr(34)."coordinates".chr(34).": [".$lng.", ".$lat."]}, ".chr(34)."type".chr(34).": ".chr(34)."Feature".chr(34).", ".chr(34)."properties".chr(34).": {}}";

echo $str;

and this is how it should look at the end according to the sample.

{"geometry": {"type": "Point", 
              "coordinates": [-104.53702657476524, 1.4311558884747997]
             }, 
              "type": "Feature", 
              "properties": {}
            }

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用leaflet.js实时。 当我编码如下坐标时,我有它工作。 我已经阅读了说明,并没有说明它想要数据的方式,而是在样本中找到了它。 如何对其进行编码以将其打印出来并且可以包含多个坐标?

任何帮助都会很棒,谢谢。

my php

  $ lat =“52.45238209999999”; 
 $ lng =“-1.743507099999988”; 
 
 $ str =“{”。chr  (34)。“geometry”.chr(34)。“:{”。chr(34)。“type”.chr(34)。“:”。chr(34)。“Point”.chr(34)。  “,”。chr(34)。“coordinates”.chr(34)。“:[”。$ lng。“,”。$ lat。“]},”。chr(34)。“type”.chr(  34)。“:”。chr(34)。“Feature”.chr(34)。“,”。chr(34)。“properties”.chr(34)。“:{}}”; 
 
echo  $ str; 
   
 
 

这是根据示例查看结尾的方式。

  {  “geometry”:{“type”:“Point”,
“坐标”:[ -  104.43702657476524,1.4311558884747997] 
},
“type”:“Feature”,
“properties”:{} 
}  
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongman5539 2018-11-19 19:08
    已采纳

    To get the output your after, it's a case of building up a multidimensional array with the right structure so that you can then json_encode() it.

    $lat = "52.45238209999999";
    $lng = "-1.743507099999988";
    
    $data = ["geometry" => ["type" => "Point",
        "coordinates" => [(float)$lng, (float)$lat]],
        "type" => "Feature",
        "properties" => (object)[]
    ];
    
    echo json_encode($data, JSON_PRETTY_PRINT);
    

    gives...

    {
        "geometry": {
            "type": "Point",
            "coordinates": [
                -1.743507099999988, 52.45238209999999
            ]
        },
        "type": "Feature",
        "properties": {}
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题