Skip to content

Instantly share code, notes, and snippets.

@BoHellgren
Created January 17, 2021 13:53
Show Gist options
  • Save BoHellgren/46fd17217592b479ebc41da454b2eb7e to your computer and use it in GitHub Desktop.
Save BoHellgren/46fd17217592b479ebc41da454b2eb7e to your computer and use it in GitHub Desktop.
import 'dart:typed_data';
import 'dart:ui';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
Future<BitmapDescriptor> createCustomMarkerBitmap(int markerNumber, String imageURL) async {
int markerWidth;
if (kIsWeb) markerWidth= 30; else markerWidth = 100;
PictureRecorder recorder = new PictureRecorder();
Canvas c = Canvas(recorder);
final data1 = await rootBundle.load(imageURL);
var markerImage = await decodeImageFromList(data1.buffer.asUint8List());
c.drawImageRect(markerImage, Rect.fromLTRB(
0.0, 0.0, markerImage.width.toDouble(), markerImage.height.toDouble()),
Rect.fromLTRB(0.0, 0.0, markerWidth.toDouble(), markerWidth.toDouble()), Paint());
TextSpan span = TextSpan(
style: TextStyle(color: Colors.black,fontSize: markerWidth/3,fontWeight: FontWeight.bold),
text: markerNumber.toString(),
);
TextPainter tp = TextPainter(text: span,textAlign: TextAlign.left,textDirection: TextDirection.ltr);
tp.layout();
tp.paint(c, Offset(markerNumber > 9 ? markerWidth/3.5 : markerWidth/2.5, markerWidth/5.5));
var p = recorder.endRecording();
ByteData pngBytes =
await (await p.toImage(markerWidth, markerWidth)).toByteData(format: ImageByteFormat.png);
Uint8List data = Uint8List.view(pngBytes.buffer);
return BitmapDescriptor.fromBytes(data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment