我正试图向谷歌地图api发送多个http请求,以获取旅程所需的时间。代码如下:
getRouteCoordinates(LatLng sourceCords, LatLng destCords)async{
String url = "https://maps.googleapis.com/maps/api/directions/json?origin=${sourceCords.latitude},${sourceCords.longitude}&destination=${destCords.latitude},${destCords.longitude}&key=$apiKey";
http.Response response = await http.get(url);
Map values = jsonDecode(response.body);
}
所以我决定用这个包[batching_future],但我似乎不明白如何使用此包使其工作。
我想用这样的目的地输入来完成这个批量请求
var inputs = [
LatLng(43.721160, 45.394435),
LatLng(23.732322, 78.385142),
LatLng(21.721160, 90.394435),
LatLng(13.732322, 59.385142),
LatLng(47.721160, 80.394435),
LatLng(25.732322, 60.385142),
];
我怎样才能做到这一点。提前谢谢。
gmaps_multitedestination包执行跟踪。
运行旅行时间批处理请求的相关代码复制并粘贴在下面:
import 'package:google_maps_webservice/distance.dart';
import 'package:meta/meta.dart';
/// Computes travel times from [myLocation] to [destinations] in batch using
/// Google Distance Matrix API and [apiKey].
///
/// Requires Google Maps API Key, Google Distance Matrix API, and is subject
/// to the limitations of the Matrix API (such as maximum destinations per
/// request)
Future<Map<Location, Duration>> batchTravelTimes(
{@required Location myLocation,
@required List<Location> destinations,
@required String apiKey}) async =>
(await GoogleDistanceMatrix(apiKey: apiKey)
.distanceWithLocation([myLocation], destinations))
.results
.expand((row) => row.elements)
.toList()
.asMap()
.map((i, location) => MapEntry(
destinations[i], Duration(seconds: location.duration.value)));
连接上述批处理_未来的代码为:
/// Computes travel times from `O` to `[D1,D2,D3]` in batch.
/// Requires Google Maps API Key, Google Distance Matrix API, and is subject
/// to the limitations of the Matrix API (such as maximum destinations per
/// request)
import 'package:batching_future/batching_future.dart';
import 'package:google_maps_webservice/distance.dart';
import 'package:meta/meta.dart';
typedef MyLocationProvider = Future<Location> Function();
BatchingFutureProvider<Location, Duration> batchingFutureTravelTime(
{@required MyLocationProvider myLocationProvider,
@required String apiKey}) =>
createBatcher(
(destinations) async => (await batchTravelTimes(
apiKey: apiKey,
myLocation: await myLocationProvider(),
destinations: destinations))
.values
.toList(),
maxBatchSize: 20,
maxWaitDuration: Duration(milliseconds: 200),
);