我需要在我的应用中创建一些标记,通过API获得纬度和经度。我将如何显示这些标记?
我的getPosts代码:
Future<List<Orders>> getPosts() async {
Response response = await http.get(url);
//print(response.body);
if(response.statusCode == 200){
List<dynamic> body = jsonDecode(response.body);
List<Orders> orders = body.map((dynamic item) => Orders.fromJson(item)).toList();
return orders;
} else {
throw "Can't get orders";
}
我的班级顺序:
class Orders {
final String id;
final String nameRestaurant;
final String address;
final String lat;
final String long;
final bool delivered;
final String orderId;
final String phone;
Orders({@required this.id, @required this.nameRestaurant, @required this.address, @required this.lat,
@required this.long, @required this.delivered, @required this.orderId, @required this.phone});
factory Orders.fromJson(Map<dynamic, dynamic> json){
return Orders(
id: json['id'] as String,
nameRestaurant: json['nameRestaurant'] as String,
address: json['address'] as String,
lat: json['lat'] as String,
long: json['long'] as String,
delivered: json['delivered'] as bool,
orderId: json['orderId'] as String,
phone: json['phone'] as String
);
}
}
我遵循了一些教程,但是所有教程都在onTap函数中添加了标记,但对我不起作用
要添加简单标记列表,请先创建标记列表:
List<Marker> _markers = <Marker>[];
然后将标记添加到此列表:
_markers.add(
Marker(
markerId: MarkerId('SomeId'),
position: LatLng(38.123,35.123),
infoWindow: InfoWindow(
title: 'The title of the marker'
)
)
);
考虑到在infoWindow
中还可以添加字幕和其他内容。最后设置谷歌地图:
GoogleMap(
initialCameraPosition: CameraPosition(
target: LatLng(38.9647,35.2233),
zoom: 9.0,
),
mapType: MapType.normal,
markers: Set<Marker>.of(_markers),
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
)