flutter_map icon indicating copy to clipboard operation
flutter_map copied to clipboard

Always got HttpException 400

Open readyiman opened this issue 1 year ago • 6 comments

Osm url https://tile.openstreetmap.org/{z}/{x}/{y}.png returns error like HttpException: Invalid statusCode: 400, uri = https://tile.openstreetmap.org/%7Bz%7D/%7Bx%7D/%7By%7D.png. Same result with mapbox.

readyiman avatar Jun 05 '24 08:06 readyiman

Can you share the code for the whole function? The one that generates the address.

xclud avatar Jun 05 '24 11:06 xclud

 late final _controller = MapController(
    location: LatLng(
      Angle.degree(initialCameraPosition.target.latitude),
      Angle.degree(initialCameraPosition.target.longitude),
    ),
  );
  late final _tilesUrl = 'https://tile.openstreetmap.org/{z}/{x}/{y}.png';

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return MapLayout(
      controller: _controller,
      builder: (context, transformer) {
        return TileLayer(
          builder: (context, x, y, z) {
            final tilesInZoom = pow(2.0, z).floor();

            while (x < 0) {
              x += tilesInZoom;
            }
            while (y < 0) {
              y += tilesInZoom;
            }

            x %= tilesInZoom;
            y %= tilesInZoom;

            return CachedNetworkImage(
              imageUrl: _tilesUrl,
              httpHeaders: {'User-Agent': packageInfo.packageName},
              fit: BoxFit.cover,
            );
          },
        );
      },
    );
  }

readyiman avatar Jun 06 '24 00:06 readyiman

Can you try another title provider? Did you check the tile addresses in your browser?

xclud avatar Jun 09 '24 18:06 xclud

I use exactly same url on flutter map, it works.

readyiman avatar Jun 10 '24 01:06 readyiman

Can you ditch httpHeaders and try again?

This is a problem with your configuration or a problem with CachedNetworkImage. Map does not download any thing from any url.

xclud avatar Jun 11 '24 20:06 xclud

Ah the problem is with your url.

late final _tilesUrl = 'https://tile.openstreetmap.org/{z}/{x}/{y}.png';

The url can't be a fixed string. It needs to be interpolated with z, x and y. Do not forget the $ sign before z, x and y. $z, $x and $y are correct.

xclud avatar Jun 11 '24 20:06 xclud