方向Api是否不接受坐标的负值?颤振



我正在向API发送方向请求,而我正面临这个问题。在坐标为flutter: start location is :latitude 37.33101308, longitude -122.03065487end 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(包:fixit_cloud_biking/dedirections/directions_repository.dart:39:26(

8个方向bloc.getDirections(包: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服务器的方向问题。所以,请求是可以的,但方法的json解码只是失败了,因为得到了一个242响应状态代码,该代码通过了检查,但由于响应主体与方法中处理的内容不匹配而失败。

最新更新