Google Maps Marker的Flutter Image叠加层

时间:2019-06-27 12:36:10

标签: flutter dart widget maps

我想在Google地图标记上显示用户图像,但是我不知道该怎么做?

在这里检查我想要的结果:

标记图像:

enter image description here

我想要的结果:

enter image description here

非常感谢

4 个答案:

答案 0 :(得分:1)

到目前为止,您还不能真正将Widget用于Marker,但这是您可以尝试的(尚未亲自测试)

var image = await BitmapDescriptor.fromAssetImage(
  ImageConfiguration(),
  "assets/images/your_image.jpg",
);

Marker marker = Marker(
  markerId: MarkerId("id"),
  icon: image,
);

答案 1 :(得分:1)

如果您要使用本地资产,就像@CopsOnRoad所说的那样,这是一种简单的方法,但是,如果您要从网络中加载,我会在Flutter map inspire in Leaflet推荐的那一刻开始使用它,知道工作是否很好。

答案 2 :(得分:0)

解决方案回购: https://github.com/amorenew/Flutter_avatar_maps

我使用了这个库:

  image: ^2.1.4

我的main.dart

import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:image/image.dart' as Images;

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();

  Future<List<int>> makeReceiptImage() async {
    // load avatar image
    ByteData imageData = await rootBundle.load('assets/av.png');
    List<int> bytes = Uint8List.view(imageData.buffer);
    var avatarImage = Images.decodeImage(bytes);

    //load marker image 
    imageData = await rootBundle.load('assets/ma.png');
    bytes = Uint8List.view(imageData.buffer);
    var markerImage = Images.decodeImage(bytes);

    //resize the avatar image to fit inside the marker image
    avatarImage = Images.copyResize(avatarImage,
        width: markerImage.width ~/ 1.1, height: markerImage.height ~/ 1.4);


    var radius = 90;
    int originX = avatarImage.width ~/ 2, originY = avatarImage.height ~/ 2;

    //draw the avatar image cropped as a circle inside the marker image
    for (int y = -radius; y <= radius; y++)
      for (int x = -radius; x <= radius; x++)
        if (x * x + y * y <= radius * radius)
          markerImage.setPixelSafe(originX + x+8, originY + y+10,
              avatarImage.getPixelSafe(originX + x, originY + y));


    return Images.encodePng(markerImage);
  }
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  List<int> myImage;
  void _incrementCounter() {
    widget.makeReceiptImage().then((List<int> image) {
      setState(() {
        myImage = image;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.brown.shade100,
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            myImage == null ? Text('fdassd') : Image.memory(myImage),
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

答案 3 :(得分:0)

我可以建议一些解决方案,其中有一些不错的库https://www.npmjs.com/package/merge-images, 因此,通常当您在标记数组上运行时,会将2张或更多图像合并到base64。这是代码段:

getMarkerImage(elementMarker, fullArray) {
    mergeImages([
      { src: './assets/img/markers/map-marker64.png', x: 0, y: 0 },
      { src: elementMarker.info.image || './assets/img/markers/markerIcon42.png', x: 11, y: 4 },
    ])
      .then(imgData => {
        console.log(imgData);
        this.markersImages.push(imgData);
        if (this.markersImages.length === this.controllersfullArray - 4) {
          fullArray.forEach((element, i) => {
            this.markers.push({
              lat: element.info.location.latitude,
              lng: element.info.location.longitude,
              controllerInfo: element.info,
              icon: {
                url: this.markersImages[i],
                labelOrigin: { x: 32, y: -10 },
                scaledSize: {
                  width: 64,
                  height: 64
                }
              },
              controllerStaus: element.status,
              label: {
                text: element.info.siteName,
                color: "#ED2C2C",
                fontWeight: "500",
                fontSize: "14px"
              }
            });
            console.log(this.markers);
          });
        }
      });
    // return (mergeImages({ src: './assets/img/markers/map-marker64.png', x: 0, y: 0 },
  }

,但是请注意,您需要使用同一域中的所有图像。因为如果不这样做,则toDataUrl会引发跨域错误