栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在google_maps_flutter Flutter插件中使用SVG标记

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在google_maps_flutter Flutter插件中使用SVG标记

这可以使用flutter_svg包来实现。

import 'dart:ui' as ui; // imported as ui to prevent conflict between ui.Image and the Image widgetimport 'package:flutter/services.dart';import 'package:flutter_svg/flutter_svg.dart';import 'package:google_maps_flutter/google_maps_flutter.dart';Future<BitmapDescriptor> _bitmapDescriptorFromSvgAsset(BuildContext context, String assetName) async {    // Read SVG file as String    String svgString = await DefaultAssetBundle.of(context).loadString(assetName);    // Create DrawableRoot from SVG String    DrawableRoot svgDrawableRoot = await svg.fromSvgString(svgString, null);    // toPicture() and toImage() don't seem to be pixel ratio aware, so we calculate the actual sizes here    MediaQueryData queryData = MediaQuery.of(context);    double devicePixelRatio = queryData.devicePixelRatio;    double width = 32 * devicePixelRatio; // where 32 is your SVG's original width    double height = 32 * devicePixelRatio; // same thing    // Convert to ui.Picture    ui.Picture picture = svgDrawableRoot.toPicture(size: Size(width, height));    // Convert to ui.Image. toImage() takes width and height as parameters    // you need to find the best size to suit your needs and take into account the    // screen DPI    ui.Image image = await picture.toImage(width, height);    ByteData bytes = await image.toByteData(format: ui.ImageByteFormat.png);    return BitmapDescriptor.fromBytes(bytes.buffer.asUint8List());}

然后,您可以照常使用BitmapDescriptor创建标记:

BitmapDescriptor bitmapDescriptor = await _bitmapDescriptorFromSvgAsset(context, 'assets/images/someimage.svg');Marker marker = Marker(markerId: MarkerId('someId'), icon: bitmapDescriptor, position: LatLng(someLatitude, someLongitude));


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/371590.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号