我正在向API发送指示请求,并且遇到了这个问题。坐标为flutter: start location is :latitude 37.33101308, longitude -122.03065487
和end location is :latitude 37.33097983, longitude -122.03063943
时,-
符号会产生问题。
发送结果请求时
http://www.yournavigation.org/api/1.0/gosmore.php?format=geojson&v=bicycle&fast=0&layer=mapnik&flat=37.33101308&flon=-122.03065487&tlat=37.33097983&tlon=-122.03063943&geometry=1&instructions=1&lang=it
我收到无法理解的错误:
[VERBOSE-2:ui_dart_state.cc(157)]未处理的异常:FormatException:意外字符(在字符1处)^
0 _ChunkedJsonParser.fail(dart:convert-patch / convert_patch.dart:1394:5)] >>
1 _ChunkedJsonParser.parseNumber(dart:convert-patch / convert_patch.dart:1261:9)
2 _ChunkedJsonParser.parse(dart:convert-patch / convert_patch.dart:926:22)
3 _parseJson(dart:convert-patch / convert_patch.dart:31:10)
4 JsonDecoder.convert(dart:convert / json.dart:495:36)
5 JsonCodec.decode(dart:convert / json.dart:153:41)
6 jsonDecode(dart:convert / json.dart:96:10)
7 DirectionsRepository.postRequest(package:fixit_cloud_biking / directions / directions_repository.dart:39:26)
8 DirectionsBloc.getDirections(package:fixit_cloud_biking / directions / directions_bloc.dart:46:12)
9 _AsyncStarStreamController.runBody(dart:async-patch / async_patch.dart:155:5)
10 _rootRun(dart:async / zone.dart:1122:3 <...>
是因为json格式问题,还是
www.yournavigation.org/api
不接受负坐标值?
如果发送的请求具有正坐标,则不会发生此错误
'http://www.yournavigation.org/api/1.0/gosmore.php?format=geojson&v=bicycle&fast=0&layer=mapnik&flat=44.5018645003438&flon=11.340018709036542&tlat=44.502138&tlon=11.340402&geometry=1&instructions=1&lang=it'
仅出于测试目的,我尝试仅使它们与.abs().
成为正坐标,但结果当然是在地图上的位置错误。是否有另一个具有所有正值的坐标系?
非常感谢您的帮助。
这是我正在从中发送请求的类:
import 'package:http/http.dart';
import 'package:latlong/latlong.dart';
import 'dart:convert' as convert;
import 'dart:math';
class DirectionsRepository {
Future<List<LatLng>> postRequest(LatLng start, LatLng end) async {
print('postRequest() called');
print(
'start location is :latitude ${start.latitude}, longitude ${start.longitude}');
print(
'end location is :latitude ${end.latitude}, longitude ${end.longitude}');
// NORMAL NEGATIVE LONGITUDE THROWS = API ERROR:
// format error [VERBOSE-2:ui_dart_state.cc(157)] Unhandled Exception: FormatException: Unexpected character (at character 1)
String flat = (start.latitude).toString();
String flon = (start.longitude).toString();
String tlat = (end.latitude).toString();
String tlon = (end.longitude).toString();
// ALL POSITIVE VALUES DON'T THROW AN ERROR BUT IS WRONG PLACE
// String flat = (start.latitude).abs().toString();
// String flon = (start.longitude).abs().toString();
// String tlat = (end.latitude).abs().toString();
// String tlon = (end.longitude).abs().toString();
final request =
'http://www.yournavigation.org/api/1.0/gosmore.php?format=geojson&v=bicycle&fast=0&layer=mapnik&flat=$flat&flon=$flon&tlat=$tlat&tlon=$tlon&geometry=1&instructions=1&lang=it';
print('final request is $request');
// working properly
// final request =
// 'http://www.yournavigation.org/api/1.0/gosmore.php?format=geojson&v=bicycle&fast=0&layer=mapnik&flat=44.5018645003438&flon=11.340018709036542&tlat=44.502138&tlon=11.340402&geometry=1&instructions=1&lang=it';
// Await the http get response, then decode the json-formatted response.
var response = await get(request);
if (response.statusCode == 200) {
var jsonResponse = convert.jsonDecode(response.body);
print('${jsonResponse.runtimeType} : $jsonResponse');
List<dynamic> coordinates = jsonResponse['coordinates'];
print('coordinates are : $coordinates');
print('coordinates are: ${coordinates.length}');
Map<String, dynamic> properties = jsonResponse['properties'];
// print('properties are $properties');
String distance = properties['distance'];
print('Route is $distance Km long.');
String instructions = properties['description'];
print('instructions are $instructions');
List<LatLng> suggestedRoute = [];
for (int i = 0; i < (coordinates.length); i++) {
dynamic coordinate = coordinates[i];
LatLng position = LatLng(coordinate[1], coordinate[0]);
suggestedRoute.add(position);
print('position is $position');
print(i);
}
print('postRequest() suggestedRoute is $suggestedRoute');
return suggestedRoute;
} else {
print(
'postRequest() Request failed with status: ${response.statusCode}.');
}
}
}
我正在向API发送指示请求,并且遇到了这个问题。坐标为颤动:开始位置是:纬度37.33101308,经度-122.03065487,结束位置是:纬度37 ....
经过几次尝试,我已将问题确定为定向API服务器问题。因此,请求很好,但是方法的json解码只是失败,因为获取了242响应状态代码,该代码通过了检查,但由于响应主体与方法中处理的内容不匹配而失败。