Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Fallback tile URLs in-case of HTTP error #1203

Closed
juanlabrador opened this issue Mar 31, 2022 · 12 comments · Fixed by #1348
Closed

[FEATURE] Fallback tile URLs in-case of HTTP error #1203

juanlabrador opened this issue Mar 31, 2022 · 12 comments · Fixed by #1348
Labels
feature This issue requests a new feature

Comments

@juanlabrador
Copy link

Describe The Problem
I'm try to catch an error callback when the tile failed, so, the idea is when the first tile url map failed, called another, but this method errorTileCallback doesn't call, so, don't find in examples an example, maybe someone know somehow? Thanks

Additional Information
TileLayerOptions getMapBoxStyle(Function refresh, {Key key}) =>
TileLayerOptions(
key: key,
urlTemplate: globalTileMap,
errorTileCallback: (tile, error) {
globalTileMap =
'https://tiles.picap.app/styles/osm-bright/{z}/{x}/{y}.png';
refresh.call();
},
backgroundColor: PiColors.backgroundTileMap);

@juanlabrador juanlabrador added bug This issue reports broken functionality or another error needs triage This new bug report needs reproducing and prioritizing labels Mar 31, 2022
@ibrierley
Copy link
Contributor

I haven't tested the errorTileCallback code....but maybe it depends how it failed ? For example, if there just isn't a response from a server, would it know ?
It may taking a bit of digging and debugging the tile_layer.dart in the layers folder to try and get an idea of what's happening.

@JaffaKetchup
Copy link
Member

Hi there @juanlabrador,
Did @ibrierley's suggestion help? Happy to help more if required :)

@juanlabrador
Copy link
Author

Hi guys, thanks for answer, well, the simple example its put a bad url, the tile when faile its return 403 error, I mean, the tile does't show map, so, in that cases, the idea is use another tile url like support, but I don't know where catch this 403 error

@JaffaKetchup
Copy link
Member

At the moment, fallback URLs aren't supported - if that's what you mean. But it sounds like an interesting and useful idea!
Converting to feature request...

@JaffaKetchup JaffaKetchup changed the title How catch error when Tile failed ? [FEATURE] Fallback tile URLs in-case of HTTP error Apr 4, 2022
@JaffaKetchup JaffaKetchup added feature This issue requests a new feature and removed bug This issue reports broken functionality or another error needs triage This new bug report needs reproducing and prioritizing labels Apr 4, 2022
@JaffaKetchup
Copy link
Member

Note that for the time being, feature requests have an indefinite wait time, as we have quite a backlog of issues and bugs to get to. Many thanks for your suggestion!

@github-actions
Copy link

github-actions bot commented May 5, 2022

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions
Copy link

github-actions bot commented Jun 8, 2022

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@TesteurManiak
Copy link
Contributor

Hi @JaffaKetchup, I think I've been able to add a fallbackUrl to the FMNetworkImageProvider, it would require to specify NetworkTileProvider() as the tile provider of TileLayer but the result is as @juanlabrador would've wanted.
If an error occured it will retry to fetch tiles with the fallback url instead.

Code Sample

TileLayer(
  urlTemplate: 'https://fake-tile-provider.org/{z}/{x}/{y}.png',
  fallbackUrl: 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
  subdomains: const ['a', 'b', 'c'],
  userAgentPackageName: 'dev.fleaflet.flutter_map.example',
  tileProvider: NetworkTileProvider(),
),
Implementation
class FMNetworkImageProvider extends ImageProvider<FMNetworkImageProvider> {
  // ...

  /// The fallback URL from which the image will be fetched.
  final String? fallbackUrl;

  // ...

  FMNetworkImageProvider(
    this.url, {
    required this.fallbackUrl,
    RetryClient? retryClient,
    this.headers = const {},
  }) : retryClient = retryClient ?? RetryClient(Client());

  // ...

  Future<ImageInfo> _loadWithRetry(
    FMNetworkImageProvider key,
    DecoderCallback decode, [
    bool useFallback = false,
  ]) async {
    assert(key == this);
    assert(useFallback == false || fallbackUrl != null);

    try {
      final uri = Uri.parse(useFallback ? fallbackUrl! : url);
      final response = await retryClient.get(uri, headers: headers);

      if (response.statusCode != 200) {
        throw NetworkImageLoadException(
            statusCode: response.statusCode, uri: uri);
      }

      final codec = await decode(response.bodyBytes);
      final image = (await codec.getNextFrame()).image;

      return ImageInfo(image: image);
    } catch (e) {
      if (!useFallback && fallbackUrl != null) {
        return _loadWithRetry(key, decode, true);
      }
      rethrow;
    }
  }
}

Would you be interested in a PR ? (I've already created a full sample added to the example app)

@JaffaKetchup
Copy link
Member

Hi @TesteurManiak,
This looks great, and we would appreciate any PR you can make :)
Just a question, why does it require NetworkTileProvider()? I think it should work for all providers if possible.
Many thanks.

@TesteurManiak
Copy link
Contributor

Thanks for the quick response, I think I can make it work for all providers, it was just easier for NetworkTileProvider as it was already relying on a RetryClient.
I'll make a PR as soon as I've finished the remaining improvements.

JaffaKetchup pushed a commit that referenced this issue Sep 17, 2022
* working for NetworkTileProvider

* working for NetworkNoRetryTileProvider

* done AssetTileProvider

* formatted comments

* fixed wording & removed openstreetmap subdomain

* removed AssetTileProvider from tile_provider_web.dart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature This issue requests a new feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants