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.
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.
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.
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.
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.
Data Control
The local tiles enable modification of map information or the loading of specific areas before overlaying on the map.
User Experience in Remote Areas
Offers map services to the clients whenever there is poor or no internet connection.
Custom Map Styles
Third-party tile providers enable you to display custom styled or any desired overlay maps as per our application.
Compliance and Privacy
Meets regulatory or privacy limitations by having tiles stored in rigid, secure locations for distribution.
To build a custom TileProvider for offline Google Maps, we’ll focus on the following:
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(),
),
},
),
);
}
}
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