Flutter

Creating a Custom TileProvider for Offline Google Maps in Flutter


Introduction

Google Maps is the most common tool to implement maps in mobile applications, but usually it works in online mode to obtain the tiles. Suppose you want to incorporate map functionality into an application that will run in an area with low or unpredictable internet connection? Here, we will build a TileProvider to use offline map tiles in Flutter using the google_maps_flutter package. This approach is most suitable when offline application is required such as hiking apps or field service tools.

 

Why is it necessary to use…?

Impeller: For developers who sometimes encounter various problems when using the default Skia rendering engine, Skia was developed for Flutter the next generation of rendering. This is overall better than the previous approach and improves most notably for applications with many animations and graphical objects.

 

Why Impeller?

  1. Offline Access

    • Users are able to use maps in conditions where there is no internet connection which is important in navigating apps or those apps that are used in/mark hiking or traveling.

  2. Improved Performance

    • A local loading of tiles means that a user will not have to wait for the tiles to be loaded from a server and hence the user experience is generally enhanced in a tile based application.

  3. Cost Reduction

    • The key recommendation made about the current use of APIs is to limit the number of API calls made to online map services so that costs that are incurred in terms of usage limits and the actual API calls made are kept to a bare minimum.

  4. Data Control

    • The local tiles enable modification of map information or the loading of specific areas before overlaying on the map.

  5. User Experience in Remote Areas

    • Offers map services to the clients whenever there is poor or no internet connection.

  6. Custom Map Styles

    • Third-party tile providers enable you to display custom styled or any desired overlay maps as per our application.

  7. Compliance and Privacy

    • Meets regulatory or privacy limitations by having tiles stored in rigid, secure locations for distribution.

Key Aspects

To build a custom TileProvider for offline Google Maps, we’ll focus on the following:

  • Understanding TileProviders: They are used to get map tiles which depict map particulars. Custom providers enable customization as demonstrated by the fetching tiles from a local storage.
  • Downloading Tiles: This format will require map tiles in .png format which are normally obtained from externals via MBTiles or downloaded locally from tile servers.
  • Setting Up a Local Cache: Save the map tiles in either local browser storage or better still, save the map tiles in the assets of the app.Integrating with Google Maps: This tutorial replaces the tiles information source with a custom TileProvider rather than using an online source.

Example

Here’s how you can implement a custom TileProvider for offline Google Maps in Flutter.

Step 1: Add Dependencies Add the google_maps_flutter package to our pubspec.yaml:

dependencies: google_maps_flutter:^2.2.0 

Step 2: Download and Store Tiles

Save the .png tiles to the app’s assets directory, maintaining a structure like assets/tiles/{zoom}/{x}/{y}.png. Update pubspec.yaml to include these

 flutter

  assets:    - assets/tiles/

 

Step 3: Create a Custom TileProvider

Implement a TileProvider that fetches tiles from the local directory.

import 'dart:io';import 'dart:typed_data';import 'package:flutter/services.dart';import 'package:google_maps_flutter/google_maps_flutter.dart';class OfflineTileProvider extends TileProvider {  @override  Future<Tile?> getTile(int x, int y, int zoom) async {    try {      // Construct the tile path      final tilePath = 'assets/tiles/$zoom/$x/$y.png';      // Load the file as Uint8List      final ByteData data = await rootBundle.load(tilePath);      return Tile(256, 256, data.buffer.asUint8List());    } catch (e) {      // Return a blank tile if not found      return null;    }  }}

 

Step 4: Use the TileProvider in Google Maps

Pass the custom TileProvider when creating the map.

import 'package:flutter/material.dart';import 'package:google_maps_flutter/google_maps_flutter.dart';class OfflineMapScreen extends StatelessWidget {  @override  Widget build(BuildContext context) {    return Scaffold(      appBar: AppBar(title: Text('Offline Google Map')),      body: GoogleMap(        initialCameraPosition: CameraPosition(          target: LatLng(37.7749, -122.4194), // San Francisco          zoom: 10,        ),        tileOverlays: {          TileOverlay(            tileOverlayId: TileOverlayId('offlineTiles'),            tileProvider: OfflineTileProvider(),          ),        },      ),    );  }} 

Conclusion

Custom TileOffline as a solution for using offline maps in Flutter is a suitable strategy for applications that work in remote locations or minimize data consumption. Still, it involves some work downloading and sorting map tiles but the end result is a very solid and intuitive offline map utility.

Use this solution in our next Flutter project and make our offline maps faster in our application. 

Ready to transform your business with our technology solutions? Contact Us today to Leverage Our Flutter Expertise.

0

Flutter

Related Center Of Excellence