| // Copyright 2013 The Flutter Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| // ignore_for_file: public_member_api_docs |
| |
| import 'dart:typed_data'; |
| import 'dart:ui' as ui; |
| |
| import 'package:flutter/material.dart'; |
| import 'package:google_maps_flutter/google_maps_flutter.dart'; |
| |
| import 'page.dart'; |
| |
| class TileOverlayPage extends GoogleMapExampleAppPage { |
| const TileOverlayPage({Key? key}) |
| : super(const Icon(Icons.map), 'Tile overlay', key: key); |
| |
| @override |
| Widget build(BuildContext context) { |
| return const TileOverlayBody(); |
| } |
| } |
| |
| class TileOverlayBody extends StatefulWidget { |
| const TileOverlayBody({Key? key}) : super(key: key); |
| |
| @override |
| State<StatefulWidget> createState() => TileOverlayBodyState(); |
| } |
| |
| class TileOverlayBodyState extends State<TileOverlayBody> { |
| TileOverlayBodyState(); |
| |
| GoogleMapController? controller; |
| TileOverlay? _tileOverlay; |
| |
| // ignore: use_setters_to_change_properties |
| void _onMapCreated(GoogleMapController controller) { |
| this.controller = controller; |
| } |
| |
| @override |
| void dispose() { |
| super.dispose(); |
| } |
| |
| void _removeTileOverlay() { |
| setState(() { |
| _tileOverlay = null; |
| }); |
| } |
| |
| void _addTileOverlay() { |
| final TileOverlay tileOverlay = TileOverlay( |
| tileOverlayId: const TileOverlayId('tile_overlay_1'), |
| tileProvider: _DebugTileProvider(), |
| ); |
| setState(() { |
| _tileOverlay = tileOverlay; |
| }); |
| } |
| |
| void _clearTileCache() { |
| if (_tileOverlay != null && controller != null) { |
| controller!.clearTileCache(_tileOverlay!.tileOverlayId); |
| } |
| } |
| |
| @override |
| Widget build(BuildContext context) { |
| final Set<TileOverlay> overlays = <TileOverlay>{ |
| if (_tileOverlay != null) _tileOverlay!, |
| }; |
| return Column( |
| mainAxisSize: MainAxisSize.min, |
| mainAxisAlignment: MainAxisAlignment.spaceEvenly, |
| crossAxisAlignment: CrossAxisAlignment.stretch, |
| children: <Widget>[ |
| Center( |
| child: SizedBox( |
| width: 350.0, |
| height: 300.0, |
| child: GoogleMap( |
| initialCameraPosition: const CameraPosition( |
| target: LatLng(59.935460, 30.325177), |
| zoom: 7.0, |
| ), |
| tileOverlays: overlays, |
| onMapCreated: _onMapCreated, |
| ), |
| ), |
| ), |
| TextButton( |
| onPressed: _addTileOverlay, |
| child: const Text('Add tile overlay'), |
| ), |
| TextButton( |
| onPressed: _removeTileOverlay, |
| child: const Text('Remove tile overlay'), |
| ), |
| TextButton( |
| onPressed: _clearTileCache, |
| child: const Text('Clear tile cache'), |
| ), |
| ], |
| ); |
| } |
| } |
| |
| class _DebugTileProvider implements TileProvider { |
| _DebugTileProvider() { |
| boxPaint.isAntiAlias = true; |
| boxPaint.color = Colors.blue; |
| boxPaint.strokeWidth = 2.0; |
| boxPaint.style = PaintingStyle.stroke; |
| } |
| |
| static const int width = 100; |
| static const int height = 100; |
| static final Paint boxPaint = Paint(); |
| static const TextStyle textStyle = TextStyle( |
| color: Colors.red, |
| fontSize: 20, |
| ); |
| |
| @override |
| Future<Tile> getTile(int x, int y, int? zoom) async { |
| final ui.PictureRecorder recorder = ui.PictureRecorder(); |
| final Canvas canvas = Canvas(recorder); |
| final TextSpan textSpan = TextSpan( |
| text: '$x,$y', |
| style: textStyle, |
| ); |
| final TextPainter textPainter = TextPainter( |
| text: textSpan, |
| textDirection: TextDirection.ltr, |
| ); |
| textPainter.layout( |
| maxWidth: width.toDouble(), |
| ); |
| textPainter.paint(canvas, Offset.zero); |
| canvas.drawRect( |
| Rect.fromLTRB(0, 0, width.toDouble(), width.toDouble()), boxPaint); |
| final ui.Picture picture = recorder.endRecording(); |
| final Uint8List byteData = await picture |
| .toImage(width, height) |
| .then((ui.Image image) => |
| image.toByteData(format: ui.ImageByteFormat.png)) |
| .then((ByteData? byteData) => byteData!.buffer.asUint8List()); |
| return Tile(width, height, byteData); |
| } |
| } |