我有一个GPS坐标数据库和有关卡车的信息,我正在尝试使用GeoJson用这些信息构建地图。我使用MYSQL收集信息,并在c#中创建GeoJson文件。我可以创建正确的GeoJson文件以显示位置,标题和小弹出窗口。然而;我想扩大该弹出窗口。在示例中,为简化起见,我将使用1辆卡车。我还更改了此示例的坐标。
我的C#输出;
{
"type":"FeatureCollection",
"features":[
{
"type":"Feature",
"properties":{
"popup":{
"title":"Truck_001",
"subtitle":"Laatst geupdate: 23.1500 uur geleden.",
"imageUrl":"<left blank for stacksocial>",
"items":{
"type":"text",
"label":"text",
"value":"text items"
}
},
"tooltip":"Laatst geupdate: 23.1500 uur geleden.",
"marker":{
"prefix":"fa",
"icon":"fa-chevron-up",
"markerColor":"#003da5",
"iconColor":"white",
"square":false,
"circle":false,
"pureIcon":false,
"layer":"layer1",
"size":"medium"
}
},
"geometry":{
"type":"Point",
"coordinates":[
1.981588,
32.1926765
]
}
}
]
}
卡车将显示在地图上,但由于缺少以下方括号而不会显示物品:“ []”
所需的输出(请参见该项目周围的“ [”和“]”)
{
"type":"FeatureCollection",
"features":[
{
"type":"Feature",
"properties":{
"popup":{
"title":"Truck_001",
"subtitle":"Laatst geupdate: 23.1500 uur geleden.",
"imageUrl":"<left blank for stacksocial>",
"items":[ /*HERE*/
{
"type":"text",
"label":"text",
"value":"text items"
}
] /* HERE */
},
"tooltip":"Laatst geupdate: 23.1500 uur geleden.",
"marker":{
"prefix":"fa",
"icon":"fa-chevron-up",
"markerColor":"#003da5",
"iconColor":"white",
"square":false,
"circle":false,
"pureIcon":false,
"layer":"layer1",
"size":"medium"
}
},
"geometry":{
"type":"Point",
"coordinates":[
1.981588,
32.1926765
]
}
}
]
}
我的C#代码:
public class Marker
{
public string prefix;
public string icon;
public string markerColor;
public string iconColor;
public bool square;
public bool circle;
public bool pureIcon;
public string layer;
public string size;
}
public class Popup
{
public string title;
public string subtitle;
public string imageUrl;
public Items items;
}
public class Items
{
public string type;
public string label;
public string value;
}
public class Properties
{
public Popup popup;
public string tooltip;
public Marker marker;
}
public class Geometry
{
public string type;
public double[] coordinates;
}
public class Feature
{
public string type;
public Properties properties;
public Geometry geometry;
}
public class GeoJSON
{
public string type;
public List<Feature> features;
}
public class SinglePosition
{
public string headingicon;
public string ts;
public string Lastupdate;
public string ticksts;
public string ticksnow;
public double lat;
public double lng;
public int heading;
public string devId;
public int heading1;
}
public static object[] Evaluate(object[] input)
{
string dbres = input[0].ToString();
var allPositions = dbres.FromJSON<SinglePosition[]>();
if (allPositions == null || allPositions.Length == 0)
return null;
var toMap = new GeoJSON() { type = "FeatureCollection", features = new List<Feature>() };
foreach (var p in allPositions)
{
if ((p.heading > 315) || (p.heading <= 45))
{
p.headingicon = "fa-chevron-up";
}
else if ((p.heading > 45) && (p.heading <= 135))
{
p.headingicon = "fa-chevron-right";
}
else if ((p.heading > 135) && (p.heading <= 225))
{
p.headingicon = "fa-chevron-down";
}
else if ((p.heading > 225) && (p.heading <= 315))
{
p.headingicon = "fa-chevron-left";
}
else
{
p.headingicon = "fa-car";
}
toMap.features.Add(new Feature()
{
type = "Feature",
properties = new Properties()
{
popup = new Popup()
{
title = p.devId,
subtitle = "Laatst geupdate: " + p.Lastupdate + " uur geleden.",
imageUrl = "<URL>",
items = new Items()
//[
{
type = "text",
label = "text",
value = "text items"
}
//]
},
tooltip = "Laatst geupdate: " + p.Lastupdate + " uur geleden.",
marker = new Marker()
{
prefix = "fa", // glyphicon or fa
icon = p.headingicon,
markerColor = "#003da5",
iconColor = "white", // #b60055
square = false,
circle = false,
pureIcon = false,
layer = "layer1",
size = "medium"
}
},
geometry = new Geometry()
{
type = "Point",
coordinates = new double[] { p.lng, p.lat }
}
});
}
return new object[] { toMap.ToJSON() };
}
我已经在线阅读了那些“ []”括号是属性,但是我不知道如何使用它们。我该如何在代码中使用它们,以便类项目使用它们?
答案 0 :(得分:1)
这些括号([]
)在JSON和C#中都表示一个数组。您可以使items
的类型为Items[]
,但这将限制您使用固定大小的数组,因此我为任务选择List<Item>
。
这应该有效:
// ...
popup = new Popup
{
title = p.devId,
subtitle = "Laatst geupdate: " + p.Lastupdate + " uur geleden.",
imageUrl = "<URL>",
items = new List<Item>
{
new Item
{
type = "text",
label = "text",
value = "text items"
}
}
}
// ...
public class Popup
{
public string title;
public string subtitle;
public string imageUrl;
public List<Item> items;
}
public class Item
{
public string type;
public string label;
public string value;
}
// ...