admin管理员组文章数量:1434934
I am working on a flutter project and have run into an issue where I am unable to make web request to the google maps directions API.
I have already tried setting the restrictions to web and adding localhost. When that didnt work, I set it to be none to see if that made a difference, but no luck. Unfortunately the response I get back using DIO isn't very helpful. Here is the error message and code sample below.
"The connection errored: The XMLHttpRequest onError callback was called. This typically indicates an error on the network layer. This indicates an error which most likely cannot be solved by the library."
Future<List<LatLng>> fetchRouteCoordinates(LatLng start, LatLng end) async {
try {
const String baseUrl =
'';
const String apiKey =
'<API Key>'; // Replace with your Google Maps API key
// final url = Uri.parse(
// '$baseUrl?origin=${start.latitude},${start.longitude}&destination=${end.latitude},${end.longitude}&key=$apiKey');
final response = await Dio(BaseOptions()).get(
'$baseUrl?origin=${start.latitude},${start.longitude}&destination=${end.latitude},${end.longitude}&key=$apiKey');
if (response.statusCode == 200) {
final data = jsonDecode(response.data);
final polyline = data['routes'][0]['overview_polyline']['points'];
return decodePolyline(polyline);
} else {
throw Exception('Failed to load directions');
}
} catch (e) {
log.e(e);
return [];
}
}
EDIT: I have tested this on both Android and in Postman and it works. Any other ideas.
EDIT 2: I was able to get around this by making an API Controller and making the request there and then returning that data.
I am working on a flutter project and have run into an issue where I am unable to make web request to the google maps directions API. https://maps.googleapis/maps/api/directions
I have already tried setting the restrictions to web and adding localhost. When that didnt work, I set it to be none to see if that made a difference, but no luck. Unfortunately the response I get back using DIO isn't very helpful. Here is the error message and code sample below.
"The connection errored: The XMLHttpRequest onError callback was called. This typically indicates an error on the network layer. This indicates an error which most likely cannot be solved by the library."
Future<List<LatLng>> fetchRouteCoordinates(LatLng start, LatLng end) async {
try {
const String baseUrl =
'https://maps.googleapis/maps/api/directions/json';
const String apiKey =
'<API Key>'; // Replace with your Google Maps API key
// final url = Uri.parse(
// '$baseUrl?origin=${start.latitude},${start.longitude}&destination=${end.latitude},${end.longitude}&key=$apiKey');
final response = await Dio(BaseOptions()).get(
'$baseUrl?origin=${start.latitude},${start.longitude}&destination=${end.latitude},${end.longitude}&key=$apiKey');
if (response.statusCode == 200) {
final data = jsonDecode(response.data);
final polyline = data['routes'][0]['overview_polyline']['points'];
return decodePolyline(polyline);
} else {
throw Exception('Failed to load directions');
}
} catch (e) {
log.e(e);
return [];
}
}
EDIT: I have tested this on both Android and in Postman and it works. Any other ideas.
EDIT 2: I was able to get around this by making an API Controller and making the request there and then returning that data.
Share Improve this question edited Nov 20, 2024 at 2:24 ThirdMovieLuke asked Nov 17, 2024 at 12:23 ThirdMovieLukeThirdMovieLuke 151 silver badge3 bronze badges2 Answers
Reset to default 1This seems like a CORS issue. In this case, you should use a backend as a proxy to execute the network requests.
Is it a Cors Policy issue maybe? You can try using this line to test:
const String proxyUrl = 'https://cors-anywhere.herokuapp/';
const String baseUrl = proxyUrl + "BASE_URL";
本文标签: Flutter WebLocalhost request to mapsgoogleapiscomStack Overflow
版权声明:本文标题:Flutter Web - Localhost request to maps.googleapis.com - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745635686a2667542.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论