简体   繁体   English

如何在颤动的谷歌地图中从互联网上获取标记图标

[英]how can get marker icon from the internet in flutter google map

I have some icon on the internet, and I want to set those icons for flutter google marker, how can I do that?我在互联网上有一些图标,我想将这些图标设置为颤振谷歌标记,我该怎么做?

One way is that get the image from the internet and convert it to byte data and Uint8List finally set it on the marker with BitmapDescriptor.fromBytes一种方法是从互联网上获取图像并将其转换为字节数据,然后 Uint8List 最终使用 BitmapDescriptor.fromBytes 将其设置在标记上

I do that but not work我这样做但不工作

someone can help?有人可以帮忙吗?

You can use 'BitmapDescriptor.fromBytes' to display icons.您可以使用“BitmapDescriptor.fromBytes”来显示图标。 According to the example below根据下面的例子

      var iconurl ='your url';
      var dataBytes;
      var request = await http.get(iconurl);
      var bytes = request.bodyBytes;

      setState(() {
        dataBytes = bytes;
      });

      final Marker marker = Marker(
        markerId: markerId,
        icon: BitmapDescriptor.fromBytes(dataBytes.buffer.asUint8List()),
        position: LatLng(
          lat,
          lng,
        ),
        infoWindow: InfoWindow(title: address, snippet: desc),
      );

You can use this approach if you want to show multiple images.如果要显示多个图像,可以使用此方法。

First Import首次导入

import 'dart:ui' as ui;


GoogleMapController controller;
  BitmapDescriptor _markerIcon;
  final LatLng _kMapCenter = const LatLng(25.2744, 133.7751);
  final List<Marker> _marker = <Marker>[];
  @override
  void initState() {
    loadData();
    super.initState();
  }

  loadData() async {
    for (var element in AppConstant.list) {
      Uint8List image = await loadNetworkImage(element['url']);
      final ui.Codec markerImageCodec = await ui.instantiateImageCodec(
          image.buffer.asUint8List(),
          targetHeight: 150,
          targetWidth: 150);
      final ui.FrameInfo frameInfo = await markerImageCodec.getNextFrame();
      final ByteData byteData =
          await frameInfo.image.toByteData(format: ui.ImageByteFormat.png);
      final Uint8List resizedImageMarker = byteData.buffer.asUint8List();

      _marker.add(Marker(
          markerId: MarkerId(element['id']),
          icon: BitmapDescriptor.fromBytes(resizedImageMarker),
          position: LatLng(
            element['lat'],
            element['lon'],
          ),
          infoWindow: InfoWindow(title: element["title"])));
    }
    setState(() {});
  }

  Future<Uint8List> loadNetworkImage(path) async {
    final completed = Completer<ImageInfo>();
    var image = NetworkImage(path);
    image.resolve(const ImageConfiguration()).addListener(
        ImageStreamListener((info, _) => completed.complete(info)));
    final imageInfo = await completed.future;
    final byteData =
        await imageInfo.image.toByteData(format: ui.ImageByteFormat.png);
    return byteData.buffer.asUint8List();
  }

  void _onMapCreated(GoogleMapController controllerParam) {
    setState(() {
      controller = controllerParam;
    });
  }

Now Initialise Google Map like this现在像这样初始化谷歌地图

GoogleMap(
          myLocationEnabled: true,
          zoomGesturesEnabled: true,
          buildingsEnabled: true,
          cameraTargetBounds: CameraTargetBounds.unbounded,
          compassEnabled: true,
          scrollGesturesEnabled: true,
          rotateGesturesEnabled: true,
          tiltGesturesEnabled: true,
          zoomControlsEnabled: true,
          minMaxZoomPreference: MinMaxZoomPreference.unbounded,
          gestureRecognizers: <Factory<OneSequenceGestureRecognizer>>{
            Factory<OneSequenceGestureRecognizer>(
              () => EagerGestureRecognizer(),
            ),
          },
          initialCameraPosition: CameraPosition(
            target: _kMapCenter,
            zoom: 2.0,
          ),
          markers: Set.from(_marker),
          onMapCreated: _onMapCreated,
        ),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM