如何将额外的文本添加到Google Map自定义标记中?

时间:2019-01-04 15:27:48

标签: google-maps flutter google-maps-markers

问题在于如何在自定义google地图标记上的文字重叠处注入代表车辆登记号码的文字。

我尝试使用此方法将文本覆盖在图标上 生成器:(上下文)=>() 但是一点都不被认可。

@keyup.enter.native="onEnter".

enter image description here

如果您注意到图标右侧的空白是我希望显示注册号的位置,那么我所显示的是图标正确显示。

2 个答案:

答案 0 :(得分:3)

尝试:

protocol Stringify {
    func stringify() -> String
}

extension Collection where Iterator.Element == Stringify {
    /// changes all the elements in the collection to a String
    func stringify() -> [String] {
        var strings = [String]()
        if let elements = self as? [Stringify] {
            for element in elements {
                strings.append(element.stringify())
            }
        }
        return strings
    }
}

extension Int: Stringify {
    func stringify() -> String {
        return String(self)
    }
}

extension Double: Stringify {
    func stringify() -> String {
        return String(self)
    }
}


let test = [5,6,7]

/// does work
[6,5,34].stringify()


/// does not work -> Error alert
test.stringify()

答案 1 :(得分:1)

我通过创建一个返回a的函数来解决/解决这个问题,该函数的返回值和解析后的值在初始化Marker时作为​​图标传入:

很抱歉,我不记得为什么我用神奇的40和20常数填充了该方法。可能是通过视觉测试计算出的,当时我当时正在渲染。

Future<BitmapDescriptor> createCustomMarkerBitmap(/* your args here */) async {

PictureRecorder recorder = new PictureRecorder();
Canvas c = new Canvas(recorder);

/* Do your painting of the custom icon here, including drawing text, shapes, etc. */

Picture p = recorder.endRecording();
ByteData pngBytes = await (await p.toImage(
    tp.width.toInt() + 40, tp.height.toInt() + 20))
    .toByteData(format: ImageByteFormat.png);

Uint8List data = Uint8List.view(pngBytes.buffer);

return BitmapDescriptor.fromBytes(data);
}

然后,在创建标记时,您可以将BitmapDescriptor作为图标传递,如下所示:

createCustomMarkerBitmap(...).then((BitmapDescriptor bitmapDescriptor) {

_markers.add(新标记( / *和其他属性:* / 图标:bitmapDescriptor, )); });

或:

BitmapDescriptor bitmapDescriptor = await createCustomMarkerBitmap(...);

标记标记=标记( / *和其他属性:* / 图标:bitmapDescriptor );

让我知道是否有帮助。 GL!

我已经找到了github问题的答案