实际上,我需要计算从我现在的位置到另一个位置的距离,我有两个点的纬度和经度。我想得到从我的点(位置)到另一点的道路距离(公里)。怎么才能做到。
注:不位移。
坚持了很多天。
发布于 2022-03-18 22:34:18
使用谷歌距离矩阵API和启用距离矩阵API从谷歌云平台控制台。
void getDistanceAndDuration(LatLng origin, LatLng destination, ResponseCallback<DistanceDurationResponse?, String?> callback) async {
try {
var apiResponse = await _client.get("https://maps.googleapis.com/maps/api/distancematrix/json?origins=${origin.latitude},${origin.longitude}"
"&destinations=${destination.latitude},${destination.longitude}&key=$mapApiKey&mode=DRIVING&",);
DistanceDurationResponse response = DistanceDurationResponse.fromJson(apiResponse.data);
var rows = response?.rows;
if(rows != null && rows.isNotEmpty) {
var elements = rows.first.elements;
if(elements != null && elements.isNotEmpty) {
var element = elements.first;
var duration = element.duration;
var distance = element.distance;
}
}
} on DioError catch (e) {
log(e.toString());
} on FormatException catch (e) {
log(e.toString());
} catch (e) {
log(e.toString());
}
}
_
class DistanceDurationResponse {
String? status;
List<Rows>? rows;
List<String>? originAddresses;
List<String>? destinationAddresses;
DistanceDurationResponse({
this.destinationAddresses,
this.originAddresses,
this.rows,
this.status,
});
DistanceDurationResponse.fromJson(dynamic json) {
destinationAddresses = json['destination_addresses'] != null ? json['destination_addresses'].cast<String>() : [];
originAddresses = json['origin_addresses'] != null ? json['origin_addresses'].cast<String>() : [];
if (json['rows'] != null) {
rows = [];
json['rows'].forEach((v) {
rows?.add(Rows.fromJson(v));
});
}
status = json['status'];
}
Map<String, dynamic> toJson() {
final map = <String, dynamic>{};
map['destination_addresses'] = destinationAddresses;
map['origin_addresses'] = originAddresses;
if (rows != null) {
map['rows'] = rows?.map((v) => v.toJson()).toList();
}
map['status'] = status;
return map;
}
}
class Rows {
List<DistanceDurationElement>? elements;
Rows({this.elements,});
Rows.fromJson(dynamic json) {
if (json['elements'] != null) {
elements = [];
json['elements'].forEach((v) {
elements?.add(DistanceDurationElement.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final map = <String, dynamic>{};
if (elements != null) {
map['elements'] = elements?.map((v) => v.toJson()).toList();
}
return map;
}
}
class DistanceDurationElement {
String? status;
EstimatedDistance? distance;
EstimatedDuration? duration;
DistanceDurationElement({
this.distance,
this.duration,
this.status,
});
DistanceDurationElement.fromJson(dynamic json) {
distance = json['distance'] != null ? EstimatedDistance.fromJson(json['distance']) : null;
duration = json['duration'] != null ? EstimatedDuration.fromJson(json['duration']) : null;
status = json['status'];
}
Map<String, dynamic> toJson() {
final map = <String, dynamic>{};
if (distance != null) {
map['distance'] = distance?.toJson();
}
if (duration != null) {
map['duration'] = duration?.toJson();
}
map['status'] = status;
return map;
}
}
class EstimatedDuration {
int? value;
String? text;
EstimatedDuration({
this.text,
this.value,
});
EstimatedDuration.fromJson(dynamic json) {
text = json['text'];
value = json['value'];
}
Map<String, dynamic> toJson() {
final map = <String, dynamic>{};
map['text'] = text;
map['value'] = value;
return map;
}
}
class EstimatedDistance {
int? value;
String? text;
EstimatedDistance({
this.text,
this.value,
});
EstimatedDistance.fromJson(dynamic json) {
text = json['text'];
value = json['value'];
}
Map<String, dynamic> toJson() {
final map = <String, dynamic>{};
map['text'] = text;
map['value'] = value;
return map;
}
}
https://stackoverflow.com/questions/71535641
复制