首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Flutter googleMap onTap longPress

Flutter googleMap onTap longPress
EN

Stack Overflow用户
提问于 2019-10-14 01:11:27
回答 1查看 1.4K关注 0票数 1

我是Flutter新手,我正在做一个tutorial to add a google map,我的代码如下:

代码语言:javascript
复制
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';


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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  Completer<GoogleMapController> _controller = Completer();



  static const LatLng _center = const LatLng(45.521563, -122.677433);

  MapType _currentMapType= MapType.normal;

  void _onMapTypeButtonPressed(){
    setState(() {
      _currentMapType = _currentMapType == MapType.normal
          ? MapType.satellite
          : MapType.normal;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: Text('Maps Sample App'),
            backgroundColor: Colors.green[700],
          ),
          body: Stack(
            children: <Widget>[
              GoogleMap(
                onMapCreated: (GoogleMapController controller) {
                  _controller.complete(controller);
                },
                initialCameraPosition: CameraPosition(
                  target: _center,
                  zoom: 11.0,
                ),
                mapType: _currentMapType,

              ),
            Padding(
              padding: const EdgeInsets.all(16.0),
              child: Align(
                alignment: Alignment.topRight,
                child: FloatingActionButton(
                  onPressed: _onMapTypeButtonPressed,
                  materialTapTargetSize: MaterialTapTargetSize.padded,
                  backgroundColor: Colors.green,
                  child: const Icon(Icons.map, size: 36.0),
                ),
              )
            )
            ],
          )
      ),
    );
  }
}

之后,教程展示了如何使用按钮添加一些标记,但我想通过在用户长按地图时添加标记来改进这一点,但我并不真正理解如何实现这一点……我发现一些文档证明了这个功能的存在,但并不是真正的如何使用它。(发现我必须在GoogleMap()小部件中添加onTap: my方法,但这不能像函数一样识别)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-14 17:45:30

第一

代码语言:javascript
复制
Set<Marker> _markers = Set();

要将其添加到地图中,请将onTap:(您的方法将获得默认的LatLng )和Marker:( _markers)添加到GoogleMap小部件

代码语言:javascript
复制
                GoogleMap(
                onMapCreated: (GoogleMapController controller) {
                  _controller.complete(controller);
                },
                initialCameraPosition: CameraPosition(
                  target: _center,
                  zoom: 11.0,
                ),
                mapType: _currentMapType,
                onTap: (LatLng latLng) {
                  _markers.add(Marker(markerId: MarkerId('mark'), position: latLng));
                  setState(() {});
                } ,
                markers: Set<Marker>.of(_markers),
              )

别忘了更改markerId,它必须是唯一的。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58365908

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档