繁体   English   中英

Flutter Google 地图:如何在运行后更改地图类型?

[英]Flutter Google Maps: How can you change maptype after runtime?

当按下如下所示的按钮时,我试图从normal更改为satellite ,但我收到一个错误,即 setMapType 不存在。

mapController.setMapType(MapType.satellite);

任何人都知道我做错了什么?

  1. 创建一个 MapType 变量:

     MapType _currentMapType = MapType.normal;
  2. 在调用您的 Google Map 小部件时引用此变量:

     googleMap = new GoogleMap( mapType: _currentMapType, //etc
  3. 创建一个浮动按钮小部件来切换 map 类型:

     floatingActionButton: FloatingActionButton( child: Icon(Icons.layers), onPressed: ()=> { setState(() { _currentMapType = (_currentMapType == MapType.normal)? MapType.satellite: MapType.normal; }); }, heroTag: null, ),
     import 'dart:async';

   import 'package:flutter/material.dart';

     import 'package:search_map_place/search_map_place.dart';
   import 'package:google_maps_flutter/google_maps_flutter.dart';

   String apiKEY;

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

 class MyApp extends StatelessWidget {
   @override
     Widget build(BuildContext context) {
      return MaterialApp(
        title: 'Search Map Place Demo',
        home: MapSample(),
         );
         }
      }

     class MapSample extends StatefulWidget {
     @override
     State<MapSample> createState() => MapSampleState();
    }

      class MapSampleState extends State<MapSample> {
      Completer<GoogleMapController> _mapController = Completer();

      final CameraPosition _initialCamera = CameraPosition(
      target: LatLng(-20.3000, -40.2990),
      zoom: 14.0000,
      );
      var maptype = MapType.normal;
       @override
       Widget build(BuildContext context) {
       return Scaffold(
       resizeToAvoidBottomPadding: false,
      body: Stack(
       children: <Widget>[
        GoogleMap(
        mapType: maptype,
        initialCameraPosition: _initialCamera,
        onMapCreated: (GoogleMapController controller) {
          _mapController.complete(controller);
        },
      ),
          
          ],
          ),

     floatingActionButton: FloatingActionButton(
    backgroundColor: Colors.white,
    foregroundColor: Colors.black,
    child: const Icon(Icons.my_location),
    onPressed: () {
    setState(() {
      
      this.maptype=MapType.satellite;
    });
    },
  ),


            );
          }
          }

暂无
暂无

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

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