Я пытаюсь получить расстояния с помощью Google Distance Matrix API с помощью следующего процесса:
- Использование местоположения пользователя в качестве источника
- Определение пункта назначения путем чтения адреса из список карт
- Проверка, если каждое расстояние меньше 15 км, и добавление имени карты и расстояния в списки
- Печать данных из списка имен и расстояний
Функция создана
void pegaDist() async {
String _resultado = "";
int contaMapas = 0;
String _ateOrigem = "https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=";
String _origem = _enderecoUsuario; //user address
String _ateDestino = "&destinations=";
String _destino = ""; //destinations
String _depoisDestino = "&key=XXXXXXXXXXXXXXXXXXXXXXXX";
double distancia = 0;
//destination
for(Map map in fornecedoresUpload){ //fornecedores = suppliers
if(contaMapas < (fornecedoresUpload.length)-1){
_destino = _destino+map["localizacao"]; //localizacao = address
_destino = _destino+"|";
contaMapas = contaMapas +1;
}else{
_destino = _destino + map["localizacao"];
}
}
//API requisition
String url = _ateOrigem +_origem + _ateDestino + _destino + _depoisDestino;
http.Response response = await http.get(url);
Map<String, dynamic> retorno = json.decode(response.body);
//verifying if the distance between the user and the supplier is less than 15 kilometers
for(int i=0 ; i< fornecedoresUpload.length; i++) {
distancia = double.tryParse(retorno["rows"][0]["elements"][i]["distance"]["value"]); // distancia = distance
if( distancia < 15000){
//adding distance and supplier name in the lists
distancias.add(distancia);
nomeFornecedoresProximos.add(fornecedoresUpload[i]["nome"]); //nome = name
}
}
for(int j =0; j< distancias.length; j++){
print("Nome: " + nomeFornecedoresProximos[j] );
print(" Distancia: " + distancias[j] );
} }
Ошибка
E/flutter (21365): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: FormatException: Unexpected character (at character 1)
E/flutter (21365): <!DOCTYPE html>
E/flutter (21365): ^
E/flutter (21365):
E/flutter (21365): #0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1394:5)
E/flutter (21365): #1 _ChunkedJsonParser.parseNumber (dart:convert-patch/convert_patch.dart:1261:9)
E/flutter (21365): #2 _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:926:22)
E/flutter (21365): #3 _parseJson (dart:convert-patch/convert_patch.dart:31:10)
E/flutter (21365): #4 JsonDecoder.convert (dart:convert/json.dart:495:36)
E/flutter (21365): #5 JsonCodec.decode (dart:convert/json.dart:153:41)
E/flutter (21365): #6 _EntradaState.pegaDist (package:solisbr/Telas/Entrada.dart:383:41)
E/flutter (21365): <asynchronous suspension>
E/flutter (21365): #7 _EntradaState.build.<anonymous closure> (package:solisbr/Telas/Entrada.dart:1095:25)
E/flutter (21365): #8 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:706:14)
E/flutter (21365): #9 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:789:36)
E/flutter (21365): #10 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter (21365): #11 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:486:11)
E/flutter (21365): #12 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:264:5)
E/flutter (21365): #13 BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:199:7)
E/flutter (21365): #14 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:467:9)
E/flutter (21365): #15 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:76:12)
E/flutter (21365): #16 PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_router.dart:117:9)
E/flutter (21365): #17 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:379:8)
E/flutter (21365): #18 PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:115:18)
E/flutter (21365): #19 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:7)
E/flutter (21365): #20 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:218:19)
E/flutter (21365): #21 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
E/flutter (21365): #22 GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter (21365): #23 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter (21365): #24 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter (21365): #25 _rootRunUnary (dart:async/zone.dart:1138:13)
E/flutter (21365): #26 _CustomZone.runUnary (dart:async/zone.dart:1031:19)
E/flutter (21365): #27 _CustomZone.runUnaryGuarded (dart:async/zone.dart:933:7)
E/flutter (21365): #28 _invoke1 (dart:ui/hooks.dart:273:10)
E/flutter (21365): #29 _dispatchPointerDataPacket (dart:ui/hooks.dart:182:5)