Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何在呼叫屏幕时显示地图_Flutter - Fatal编程技术网

Flutter 如何在呼叫屏幕时显示地图

Flutter 如何在呼叫屏幕时显示地图,flutter,Flutter,我一直在使用map_view:^0.0.14插件在Flatter中显示地图。show()方法不返回小部件。它返回void,以便调用show()方法和display map。我想在页面加载时显示地图。我该怎么做呢 @override Widget build(BuildContext context) { return new Scaffold( appBar: new AppBar( title: new Text('Google Maps'),

我一直在使用map_view:^0.0.14插件在Flatter中显示地图。show()方法不返回小部件。它返回void,以便调用show()方法和display map。我想在页面加载时显示地图。我该怎么做呢

@override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Google Maps'),
      ),
      body: new Center(
        child: Container(
          child: RaisedButton(
            child: Text('Tap me'),
            color: Colors.blue,
            textColor: Colors.white,
            elevation: 7.0,
            onPressed: ( ) {
              mapView.show(
                  new MapOptions(
                      mapViewType: MapViewType.normal,
                      showMyLocationButton: true,
                      showCompassButton: true,
                      showUserLocation: true,
                      initialCameraPosition: new CameraPosition(new Location(11.6643, 78.1460), 14.0),
                      title: 'Google Map'));
          },
          ),
        ),
      ),
    );
  }

据我所知,map_view没有小部件。 您可以尝试一下,它有一个小部件,让您可以通过控制器进行平移或缩放


您首先需要在应用程序清单(Android)中指定API密钥:

在iOS上,您还必须使用键
io.flatter.embedded\u views\u preview
和值
YES
将布尔值添加到
Info.plist

然后,您可以将GoogleMap小部件添加到小部件树中:

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

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

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

class MapsDemoState extends State<MapsDemo> {

  GoogleMapController mapController;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(15.0),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: <Widget>[
          Center(
            child: SizedBox(
              width: 300.0,
              height: 200.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; });
  }
}
导入“包装:颤振/材料.省道”;
导入“包:google_-maps_-flatter/google_-maps_-flatter.dart”;
void main(){
runApp(材料应用程序)(
家:脚手架(
appBar:appBar(标题:const Text(“谷歌地图演示”),
正文:MapsDemo(),
),
));
}
类MapsDemo扩展StatefulWidget{
@凌驾
State createState()=>MapsDemoState();
}
类MapsDemoState扩展了状态{
谷歌地图控制器;
@凌驾
小部件构建(构建上下文){
返回填充(
填充:所有边缘设置(15.0),
子:列(
mainAxisAlignment:mainAxisAlignment.space,
儿童:[
居中(
孩子:大小盒子(
宽度:300.0,
高度:200.0,
孩子:谷歌地图(
onMapCreated:_onMapCreated,
),
),
),
升起的按钮(
child:const Text(“去伦敦”),
onPressed:mapController==null?null:(){
mapController.animateCamera(CameraUpdate.newCameraPosition(
常量摄影机位置(
轴承:270.0,
目标:拉丁美洲(51.5160895,-0.1294527),
倾斜:30.0,
缩放:17.0,
),
));
},
),
],
),
);
}
void\u onMapCreated(谷歌地图控制器){
setState((){mapController=controller;});
}
}

据我所知,地图视图没有小部件。 您可以尝试一下,它有一个小部件,让您可以通过控制器进行平移或缩放


您首先需要在应用程序清单(Android)中指定API密钥:

在iOS上,您还必须使用键
io.flatter.embedded\u views\u preview
和值
YES
将布尔值添加到
Info.plist

然后,您可以将GoogleMap小部件添加到小部件树中:

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

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

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

class MapsDemoState extends State<MapsDemo> {

  GoogleMapController mapController;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(15.0),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: <Widget>[
          Center(
            child: SizedBox(
              width: 300.0,
              height: 200.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; });
  }
}
导入“包装:颤振/材料.省道”;
导入“包:google_-maps_-flatter/google_-maps_-flatter.dart”;
void main(){
runApp(材料应用程序)(
家:脚手架(
appBar:appBar(标题:const Text(“谷歌地图演示”),
正文:MapsDemo(),
),
));
}
类MapsDemo扩展StatefulWidget{
@凌驾
State createState()=>MapsDemoState();
}
类MapsDemoState扩展了状态{
谷歌地图控制器;
@凌驾
小部件构建(构建上下文){
返回填充(
填充:所有边缘设置(15.0),
子:列(
mainAxisAlignment:mainAxisAlignment.space,
儿童:[
居中(
孩子:大小盒子(
宽度:300.0,
高度:200.0,
孩子:谷歌地图(
onMapCreated:_onMapCreated,
),
),
),
升起的按钮(
child:const Text(“去伦敦”),
onPressed:mapController==null?null:(){
mapController.animateCamera(CameraUpdate.newCameraPosition(
常量摄影机位置(
轴承:270.0,
目标:拉丁美洲(51.5160895,-0.1294527),
倾斜:30.0,
缩放:17.0,
),
));
},
),
],
),
);
}
void\u onMapCreated(谷歌地图控制器){
setState((){mapController=controller;});
}
}

能否为您的答案提供更多详细信息或文档?我已使用软件包自述文件中提供的文档更新了答案。谢谢。它工作正常,但我想捕获marker事件。您能为我们提供更多详细信息或您的答案的文档吗?我已经用软件包自述中提供的文档更新了答案。谢谢。它工作正常,但我想捕捉标记事件。
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

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

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

class MapsDemoState extends State<MapsDemo> {

  GoogleMapController mapController;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(15.0),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: <Widget>[
          Center(
            child: SizedBox(
              width: 300.0,
              height: 200.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; });
  }
}