更改谷歌地图标记颤振的图标大小

Dan*_*l.V 25 google-maps dart flutter

我google_maps_flutter在我的扑动应用程序中使用谷歌地图我有自定义标记图标,我加载此BitmapDescriptor.fromAsset("images/car.png")但我在地图上的图标大小太大我想让它变小但我找不到任何选项,这是有任何选项更改自定义标记图标.这是我的扑动代码:

mapController.addMarker(
        MarkerOptions(
          icon: BitmapDescriptor.fromAsset("images/car.png"),

          position: LatLng(
            deviceLocations[i]['latitude'],
            deviceLocations[i]['longitude'],
          ),
        ),
      );
Run Code Online (Sandbox Code Playgroud)

这是我的android模拟器的截图:

正如您在图片中看到的,我的自定义图标大小太大了

Mig*_*ivo 33

TL; DR:只要能够将任何图像编码为原始字节(例如)Uint8List,就可以将其用作标记。


截至目前,您可以使用Uint8List数据在Google Maps中创建标记。也就是说,只要您保持正确的编码格式(在此特定情况下为),就可以使用原始数据将所需的内容绘制为地图标记png。

我将通过两个示例来说明您可以:

  1. 选择一个本地资产并将其大小动态更改为所需的大小,然后将其呈现在地图上(Flutter徽标图像);
  2. 在画布上绘制一些东西,并将其作为标记进行渲染,但这可以是任何渲染小部件。

除此之外,您甚至可以将渲染窗口小部件转换为静态图像,因此也可以将其用作标记。


1.使用资产

首先,创建一种处理资产路径并接收尺寸的方法(可以是宽度,高度或两者均可,但是仅使用一个将保留比率)。

import 'dart:ui' as ui;

Future<Uint8List> getBytesFromAsset(String path, int width) async {
  ByteData data = await rootBundle.load(path);
  ui.Codec codec = await ui.instantiateImageCodec(data.buffer.asUint8List(), targetWidth: width);
  ui.FrameInfo fi = await codec.getNextFrame();
  return (await fi.image.toByteData(format: ui.ImageByteFormat.png)).buffer.asUint8List();
}
Run Code Online (Sandbox Code Playgroud)

然后,使用正确的描述符将其添加到地图中:

final Uint8List markerIcon = await getBytesFromAsset('assets/images/flutter.png', 100);
final Marker marker = Marker(icon: BitmapDescriptor.fromBytes(markerIcon));
Run Code Online (Sandbox Code Playgroud)

这将分别产生50、100和200宽度的以下内容。

asset_example


2.使用画布

您可以使用画布绘制任何想要的东西,然后将其用作标记。下面将产生一些简单的带有Hello world!文本的圆形框。

因此,首先使用画布绘制一些东西:

Future<Uint8List> getBytesFromCanvas(int width, int height) async {
  final ui.PictureRecorder pictureRecorder = ui.PictureRecorder();
  final Canvas canvas = Canvas(pictureRecorder);
  final Paint paint = Paint()..color = Colors.blue;
  final Radius radius = Radius.circular(20.0);
  canvas.drawRRect(
      RRect.fromRectAndCorners(
        Rect.fromLTWH(0.0, 0.0, width.toDouble(), height.toDouble()),
        topLeft: radius,
        topRight: radius,
        bottomLeft: radius,
        bottomRight: radius,
      ),
      paint);
  TextPainter painter = TextPainter(textDirection: TextDirection.ltr);
  painter.text = TextSpan(
    text: 'Hello world',
    style: TextStyle(fontSize: 25.0, color: Colors.white),
  );
  painter.layout();
  painter.paint(canvas, Offset((width * 0.5) - painter.width * 0.5, (height * 0.5) - painter.height * 0.5));
  final img = await pictureRecorder.endRecording().toImage(width, height);
  final data = await img.toByteData(format: ui.ImageByteFormat.png);
  return data.buffer.asUint8List();
}
Run Code Online (Sandbox Code Playgroud)

然后以相同的方式使用它,但是这次提供的是您想要的任何数据(例如宽度和高度),而不是资产路径。

final Uint8List markerIcon = await getBytesFromCanvas(200, 100);
final Marker marker = Marker(icon: BitmapDescriptor.fromBytes(markerIcon));
Run Code Online (Sandbox Code Playgroud)

在这里,你有它。

canvas_example

  • 这是一个很好的解决方案,但标记在 Google Maps for Flutter 中的实现非常糟糕。看看 React 版本、组件或替代的 Map 插件,它们的处理要好得多(Marker 只是一个带有经纬度的小部件;谁关心孩子是什么。) (2认同)

Lim*_*ver 6

给出的所有答案都是完美的,但我注意到,当您将 设定targetWidth为指定数字时,您可能会遇到具有不同devicePixelRatio. 这就是我的实现方式。

import 'dart:ui' as ui;
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';


  Future<Uint8List> getBytesFromAsset(String path) async {
    double pixelRatio = MediaQuery.of(context).devicePixelRatio;
    ByteData data = await rootBundle.load(path);
    ui.Codec codec = await ui.instantiateImageCodec(
        data.buffer.asUint8List(),
        targetWidth: pixelRatio.round() * 30
    );
    ui.FrameInfo fi = await codec.getNextFrame();
    return (await fi.image.toByteData(format: ui.ImageByteFormat.png)).buffer.asUint8List();
  }

Run Code Online (Sandbox Code Playgroud)

并使用这样的方法

final Uint8List markerIcon = await getBytesFromAsset('assets/images/bike.png');

Marker(icon: BitmapDescriptor.fromBytes(markerIcon),)
Run Code Online (Sandbox Code Playgroud)

这给了我一个动态大小,具体取决于devicePixelRatio.

这对我来说非常有效。


小智 5

我有同样的问题,我是这样解决的。

Future < Uint8List > getBytesFromCanvas(int width, int height, urlAsset) async 
{
    final ui.PictureRecorder pictureRecorder = ui.PictureRecorder();
    final Canvas canvas = Canvas(pictureRecorder);
    final Paint paint = Paint()..color = Colors.transparent;
    final Radius radius = Radius.circular(20.0);
    canvas.drawRRect(
        RRect.fromRectAndCorners(
            Rect.fromLTWH(0.0, 0.0, width.toDouble(), height.toDouble()),
            topLeft: radius,
            topRight: radius,
            bottomLeft: radius,
            bottomRight: radius,
        ),
        paint);

    final ByteData datai = await rootBundle.load(urlAsset);

    var imaged = await loadImage(new Uint8List.view(datai.buffer));

    canvas.drawImage(imaged, new Offset(0, 0), new Paint());

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

Future < ui.Image > loadImage(List < int > img) async {
    final Completer < ui.Image > completer = new Completer();
    ui.decodeImageFromList(img, (ui.Image img) {

        return completer.complete(img);
    });
    return completer.future;
}
Run Code Online (Sandbox Code Playgroud)

你可以这样使用。

final Uint8List markerIcond = await getBytesFromCanvas(80, 98, urlAsset);

setState(() {

    markersMap[markerId] = Marker(
        markerId: MarkerId("marker_${id}"),
        position: LatLng(double.parse(place.lat), double.parse(place.lng)),

        icon: BitmapDescriptor.fromBytes(markerIcond),
        onTap: () {
            _onMarkerTapped(placeRemote);
        },

    );
});
Run Code Online (Sandbox Code Playgroud)


小智 5

我已经更新了上面的功能,现在您可以根据需要缩放图像了。

  Future<Uint8List> getBytesFromCanvas(int width, int height, urlAsset) async {
    final ui.PictureRecorder pictureRecorder = ui.PictureRecorder();
    final Canvas canvas = Canvas(pictureRecorder);

    final ByteData datai = await rootBundle.load(urlAsset);
    var imaged = await loadImage(new Uint8List.view(datai.buffer));
    canvas.drawImageRect(
      imaged,
      Rect.fromLTRB(
          0.0, 0.0, imaged.width.toDouble(), imaged.height.toDouble()),
      Rect.fromLTRB(0.0, 0.0, width.toDouble(), height.toDouble()),
      new Paint(),
    );

    final img = await pictureRecorder.endRecording().toImage(width, height);
    final data = await img.toByteData(format: ui.ImageByteFormat.png);
    return data.buffer.asUint8List();
  }
Run Code Online (Sandbox Code Playgroud)


Ian*_*ith 5

这是 2020 年 5 月添加自定义 Google 地图标记的示例。

我的示例应用程序:

进口:

import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:flutter/services.dart';
import 'package:flutter/material.dart';
Run Code Online (Sandbox Code Playgroud)

在您的主要有状态类中的某处实例化您的标记地图:

Map<MarkerId, Marker> markers = <MarkerId, Marker>{};
Run Code Online (Sandbox Code Playgroud)

将图标资产转换为 Uint8List 对象的函数(根本不复杂 /s):

Future<Uint8List> getBytesFromAsset(String path, int width) async {
    ByteData data = await rootBundle.load(path);
    ui.Codec codec =
        await ui.instantiateImageCodec(data.buffer.asUint8List(), targetWidth: width);
    ui.FrameInfo fi = await codec.getNextFrame();
    return (await fi.image.toByteData(format: ui.ImageByteFormat.png)).buffer.asUint8List();
   }
Run Code Online (Sandbox Code Playgroud)

添加标记功能(在您想要标记的位置使用您的纬度和经度坐标调用它)

  Future<void> _addMarker(tmp_lat, tmp_lng) async {
    var markerIdVal = _locationIndex.toString();
    final MarkerId markerId = MarkerId(markerIdVal);
    final Uint8List markerIcon = await getBytesFromAsset('assets/img/pin2.png', 100);

    // creating a new MARKER
    final Marker marker = Marker(
      icon: BitmapDescriptor.fromBytes(markerIcon),
      markerId: markerId,
      position: LatLng(tmp_lat, tmp_lng),
      infoWindow: InfoWindow(title: markerIdVal, snippet: 'boop'),
    );

    setState(() {
      // adding a new marker to map
      markers[markerId] = marker;
    });
  }
Run Code Online (Sandbox Code Playgroud)

pubspec.yaml(随意尝试不同的图标)

flutter:

  uses-material-design: true

  assets:
    - assets/img/pin1.png
    - assets/img/pin2.png
Run Code Online (Sandbox Code Playgroud)