我正在尝试建立一个显示带有路线的开放街道地图的页面。我已经设置了OSM,并且应该通过LatLng对象列表(一个由两个双精度线组成的对象组成的对象,以标记由一条线连接的点的经度和纬度)添加路线/折线。我要做的是获取用户的位置,然后通过使用Graphhopper API获取从用户位置到其他位置的路径的路径的纬度和经度。
从API返回的JSON如下:
{
"hints":{
"visited_nodes.average":"40.0",
"visited_nodes.sum":"40"
},
"info":{
"copyrights":[
"GraphHopper",
"OpenStreetMap contributors"
],
"took":5
},
"paths":[
{
"distance":689.229,
"weight":408.670174,
"time":496240,
"transfers":0,
"points_encoded":false,
"bbox":[
15.23345,
44.103858,
15.238698,
44.105704
],
"points":{
"type":"LineString",
"coordinates":[
[
15.238079,
44.103858
],
[
15.238369,
44.104135
],
[
15.238698,
44.104337
],
[
15.238349,
44.104658
],
[
15.238155,
44.104889
],
[
15.237904,
44.105114
],
[
15.237713,
44.105236
],
[
15.237051,
44.105388
],
[
15.236858,
44.105457
],
[
15.236894,
44.105388
],
[
15.236866,
44.105314
],
[
15.236739,
44.105209
],
[
15.235663,
44.104713
],
[
15.234928,
44.105129
],
[
15.234886,
44.105037
],
[
15.234913,
44.10476
],
[
15.234786,
44.10476
],
[
15.234449,
44.105039
],
[
15.23355,
44.105704
],
[
15.23345,
44.105639
]
]
},
"legs":[
],
"details":{
},
"ascend":2.619999408721924,
"descend":3.4739990234375,
"snapped_waypoints":{
"type":"LineString",
"coordinates":[
[
15.238079,
44.103858
],
[
15.23345,
44.105639
]
]
}
}
]
}
基本上,我需要从此JSON创建一个LatLng对象的列表(示例:[LatLng(15.236866, 44.105314), LatLng(15.23355, 44.105704)]
),但是可悲的是,我不知道如何执行此操作。任何帮助,建议或指导,将不胜感激。
我尝试过在网上搜索并一起破解一些代码,但恐怕它并没有太大帮助。
Future<Points> _fetchCoordinates() async {
final response = await http.get(
'https://graphhopper.com/api/1/route?point=44.1035042,15.2385878&point=44.105091,15.2318734&vehicle=foot&locale=hr&key=<API_KEY>&points_encoded=false&instructions=false',
);
if (response.statusCode == 200) {
return Points.fromJson(json.decode(response.body));
} else {
throw Exception('Error');
}
}
class Points {
List<List<double>> coordinates;
Points({this.coordinates});
factory Points.fromJson(Map<String, dynamic> json) => Points(
coordinates: List<List<double>>.from(json["paths"]["points"]
["coordinates"]
.map((x) => List<double>.from(x.map((x) => x.toDouble())))));
Map<String, dynamic> toJson() => {
"coordinates": List<dynamic>.from(
coordinates.map((x) => List<dynamic>.from(x.map((x) => x))))
};
}
class Routing extends StatelessWidget {
@override
Widget build(BuildContext context) {
return FutureBuilder<Points>(
future: _fetchCoordinates(),
builder: (context, snapshot) {
final List coordinates =
snapshot.hasData ? snapshot.data.coordinates : <LatLng>[];
if (snapshot.hasData) {
return MyCustomMap(
lat: 44.1035042,
lng: 15.2385878,
points: <List of LatLng objects, for example: [LatLng(41.234,
43.465)]>,
);
} else if (snapshot.hasError) {
return [...]
} else
return [...]
},
);
}
}
我的代码似乎有错误;返回的语句位于“ else if(snapshot.hasError)”子句中。
答案 0 :(得分:0)
像这样定义LatLng
类:
class LatLng {
double lat;
double lng;
LatLng(this.lat, this.lng);
}
然后您可以像这样解码json:
Map<String, dynamic> decoded = json.decode(j);
List<dynamic> co1 = decoded['paths'][0]['points']['coordinates'];
List<LatLng> coords = co1.map((pair) => LatLng(pair[0], pair[1])).toList();
您仍然可以使用Points
类来包装List<LatLng>
...