【问题标题】:How to add extra into text into flutter google map custom marker?如何将额外的文本添加到颤动的谷歌地图自定义标记中?
【发布时间】:2019-05-31 04:42:21
【问题描述】:

问题是如何在自定义谷歌地图标记上使用代表车辆登记号的文本注入重叠文本。

我曾尝试使用此方法在图标上叠加文字 建设者:(上下文)=>() 但根本不认识。

  class MapsDemo extends StatefulWidget {
    @override
    State createState() => MapsDemoState();
  }

class MapsDemoState extends State<MapsDemo> {

  GoogleMapController mapController;
 //Map<PermissionGroup, PermissionStatus> permissions = await PermissionHandler().requestPermissions([PermissionGroup.contacts]);import 'package:permission_handler/permission_handler.dart';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
          body: Column(
            children: <Widget>[
            Container(
                height: MediaQuery.of(context).size.height,
                width: MediaQuery.of(context).size.width,
                child: GoogleMap(
                 onMapCreated: (GoogleMapController controller) {
                   mapController = controller;
                 },
                ),
            ),
           ],
         ),
         floatingActionButton: FloatingActionButton(onPressed: () {
        
          double mq1 = MediaQuery.of(context).devicePixelRatio;
          String icon = "images/car.png";
          if (mq1>1.5 && mq1<2.5) {icon = "images/car2.png";}
          else if(mq1 >= 2.5){icon = "images/car3.png";}
          print("Mq 1"+mq1.toStringAsFixed(5));
          String iconPath="lib/assets/move@3x.png";
          
          
         mapController.addMarker(
              MarkerOptions(
                position: LatLng(37.4219999, -122.0862462),
                infoWindowText: InfoWindowText("TEST","TEST"),
                icon: BitmapDescriptor.fromAsset(iconPath),
                consumeTapEvents: true,
                
                /*builder: (context) =>(

                )*/

                //icon:BitmapDescriptor.fromAsset(assetName)
              ),
         );
         
         mapController.addMarker(
              MarkerOptions(
                position: LatLng(38.4219999, -122.0862462),
                infoWindowText: InfoWindowText("tt","adfaf"),
                icon: BitmapDescriptor.fromAsset("lib/assets/logo.png"),
                anchor: Offset(100,160),
              
                //icon:BitmapDescriptor.fromAsset(assetName)
              ),
         );
       
         mapController.animateCamera(
            CameraUpdate.newCameraPosition(
              CameraPosition(
                target: LatLng(37.4219999, -122.0862462),
               
                zoom: 15.0,
              ),
            ),
          );
       
        })
      );
    
  }
  
}

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

【问题讨论】:

  • 能否将截图编辑成问题?最好包括回答问题所需的所有内容:stackoverflow.com/help/mcve
  • @MiloP 我不知道要在屏幕截图上编辑什么?不可见吗?
  • @anmol.majhail 知道如何动态地将文本带入空白区域以防抖动,我需要非常糟糕地推进应用程序。我什至试图让窗口文本永久打开,也不管用
  • @newbie 你检查过这个例子吗? github.com/flutter/plugins/blob/master/packages/…这个例子是正常使用InfoWindowText的。
  • @aubykhan 是的,我什至在 infoWindowText 上面的示例代码中使用过:InfoWindowText("TEST","TEST"),但是只有在我点击图标时才会出现问题。如果可能,我希望所有图标的信息窗口自动打开?现在可以吗?

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


【解决方案1】:

试试这个:

 import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

class MapsDemo extends StatefulWidget {
  @override
  State createState() => MapsDemoState();
}

class MapsDemoState extends State<MapsDemo> {
  final Set<Marker> _markers = {};

  void _onAddMarkerButtonPressed() {
    print('in _onAddMarkerButtonPressed()');
    setState(() {
      _markers.add(Marker(
        // This marker id can be anything that uniquely identifies each marker.
        markerId: MarkerId("111"),
        position: LatLng(30.666, 76.8127),
        infoWindow: InfoWindow(
          title: "bingo! This works",
        ),
        icon: BitmapDescriptor.defaultMarker,
      ));
    });
    print('setState() done');
  }

  GoogleMapController mapController;
  //Map<PermissionGroup, PermissionStatus> permissions = await PermissionHandler().requestPermissions([PermissionGroup.contacts]);import 'package:permission_handler/permission_handler.dart';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Column(
          children: <Widget>[
            Container(
              height: MediaQuery.of(context).size.height,
              width: MediaQuery.of(context).size.width,
              child: GoogleMap(
                markers: _markers,
                onMapCreated: (GoogleMapController controller) {
                  mapController = controller;
                },
                initialCameraPosition:
                    CameraPosition(target: LatLng(30.666, 76.8127), zoom: 15),
              ),
            ),
          ],
        ),
        floatingActionButton: FloatingActionButton(onPressed: () {
          print('in fab()');
          double mq1 = MediaQuery.of(context).devicePixelRatio;



          _onAddMarkerButtonPressed();

          mapController.animateCamera(
            CameraUpdate.newCameraPosition(
              CameraPosition(
                target: LatLng(30.666, 76.8127),
                zoom: 15.0,
              ),
            ),
          );
        }));
  }
}

【讨论】:

  • 注意:我已经删除了您的部分代码,并专注于在标记点按时显示覆盖的文本。
  • 但是在我的问题中,这并没有按照我在上图中的要求显示标签,对吗?你能在我的图标中看到我想要显示标签的地方吗?
  • 你是对的。检查“覆盖小部件”。这将达到目的。同时,我会在今天的某个时间尝试发布另一个版本的代码。
  • 很遗憾,marker 不支持覆盖。但是,我们可以在 GoogleMaps 上显示叠加层。你可以做的是在底部(即谷歌地图下方)显示车辆列表,当用户点击列表视图时,我们可以根据需要显示文本。示例实现(使用 TextFormField 而不是 ListTile)pastebin.com/hQr5HBV2
  • 我明白你的想法,但要求是让他永久地“覆盖”文本,以便用户一眼就能看到车辆,而无需点击或单击。
【解决方案2】:

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

很抱歉,我不记得为什么用神奇的 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);
}

那么在创建marker的时候,可以传入BitmapDescriptor作为icon,像这样:

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

_markers.add(新标记( /* 除了你的其他属性:*/ 图标:位图描述符, )); });

或:

BitmapDescriptor bitmapDescriptor = await createCustomMarkerBitmap(...);

标记标记=标记( /* 除了你的其他属性:*/ 图标:位图描述符 );

如果有帮助,请告诉我。哥!

我找到了这个 github 问题的答案

【讨论】:

  • 如何用我现有的图标调用这个函数?我不太明白
  • 能否给出完整代码或获取图片并在其上绘制文字?
猜你喜欢
  • 1970-01-01
  • 2021-11-15
  • 2019-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-04
  • 1970-01-01
相关资源
最近更新 更多