gpt4 book ai didi

google-maps - 在 Flutter Google Maps Plugin 中的 Marker _icon 下放置自定义文本

转载 作者:行者123 更新时间:2023-12-05 09:13:25 25 4
gpt4 key购买 nike

我正在尝试向自定义标记添加一些文本(我正在使用 google maps flutter )

var cluster =  Marker(
markerId: MarkerId( uuid.v1() ),
position: new LatLng(clusterMap[box].latitude,clusterMap[box].longitude ),
icon: _clusterImage,
alpha: 0.5);

我能做的只是改变图标,现在图标是一个灰色背景的圆圈。我想在这个圈子里添加一些文字。有办法吗?

最佳答案

您需要使用 Canvas 在记号笔上书写。此函数在标记上书写。将这些行添加到您的类(class)后,请往下看。

import 'package:flutter/material.dart'; 
import 'package:flutter/painting.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'dart:async';
import 'dart:typed_data';
import 'dart:ui' as ui;

....... .......
Future<Uint8List> getBytesFromCanvas(String text) async {

final ui.PictureRecorder pictureRecorder = ui.PictureRecorder();
final Canvas canvas = Canvas(pictureRecorder);
final Paint paint1 = Paint()..color = Colors.grey;
final int size = 100; //change this according to your app
canvas.drawCircle(Offset(size / 2, size / 2), size / 2.0, paint1);
TextPainter painter = TextPainter(textDirection: TextDirection.ltr);
painter.text = TextSpan(
text: text,//you can write your own text here or take from parameter
style: TextStyle(
fontSize: size / 4, color: Colors.black, fontWeight: FontWeight.bold),
);
painter.layout();
painter.paint(
canvas,
Offset(size / 2 - painter.width / 2, size / 2 - painter.height / 2),
);

final img = await pictureRecorder.endRecording().toImage(size, size);
final data = await img.toByteData(format: ui.ImageByteFormat.png);
return data.buffer.asUint8List();
}

现在,您可以通过以下方式轻松调用您想要的标记。

final Uint8List desiredMarker =
await getBytesFromCanvas('Write What You want!!');

var cluster = Marker(
markerId: MarkerId( uuid.v1() ),
position: new LatLng(clusterMap[box].latitude,clusterMap[box].longitude ),
icon: BitmapDescriptor.fromBytes(desiredMarker),
alpha: 0.5);

希望对您有所帮助。保重。

关于google-maps - 在 Flutter Google Maps Plugin 中的 Marker _icon 下放置自定义文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56172621/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com