第一个扑扑的应用程序。尝试从小部件主屏幕调用方法

时间:2018-12-14 23:24:47

标签: flutter

我使用flutter_google_maps插件从互联网上获取了一个应用程序。为此,我添加了一种名为centermap的方法。我已经尝试了无数种尝试,并尝试使该应用读取中心地图并将google地图重新定位到坐标。我曾尝试使用谷歌搜索,但在Flutter中找不到执行此操作的任何示例。

可以帮助我前进。在原始代码中,我复制了凸起的按钮。

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

GoogleMapController mapController;

void main() {
  runApp(MaterialApp(
    home: Scaffold(
      appBar: AppBar(title: const Text('Google Maps demo')),
      body: MapsDemo(),
    ),
  ));

  centerMap(mapController);
}

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

}

class MapsDemoState extends State<MapsDemo> {

  @override
  Widget build(BuildContext context) {
    return Padding (
      padding: EdgeInsets.all(15.0),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: <Widget>[
          Center(
            child: SizedBox(
              width: 400.0,
              height: 500.0,
              child: GoogleMap(
                onMapCreated: _onMapCreated,
              ),

            ),
          ),
          RaisedButton(
              child: const Text('Go to London'),
              onPressed: mapController ==  null ? null : (){
                mapController.animateCamera(CameraUpdate.newCameraPosition(
                  const CameraPosition(
                    bearing: 270.0,
                    target: LatLng(51.5160895, -0.1294527),
                    tilt: 30.0,
                    zoom: 17.0,
                  ),
                ));
              },
          ),
        ],
      ),
    );
  }

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

void centerMap(GoogleMapController mapController) {
  mapController.animateCamera(
    CameraUpdate.newCameraPosition(
      CameraPosition(
          target: LatLng(37.4219999, -122.0862462), zoom: 20.0),
    ),
  );
}

1 个答案:

答案 0 :(得分:0)

我应该继续尝试几分钟

这似乎可行

void _onMapCreated(GoogleMapController controller) {
    setState(() {
      mapController = controller;
    });
    centerMap(mapController);
  }
}

void centerMap(GoogleMapController mapController) {
  mapController.animateCamera(
    CameraUpdate.newCameraPosition(
      CameraPosition(
          target: LatLng(37.4219999, -122.0862462), zoom: 20.0),
    ),
  );
}