通话时如何显示地图

时间:2019-01-29 08:48:25

标签: flutter

我一直在使用map_view:^ 0.0.14插件来在抖动中显示地图。 Mapview.show()方法不返回小部件。它返回void所以我可以调用show()方法并显示地图。我想在页面加载时显示地图。我该怎么办。

@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'));
          },
          ),
        ),
      ),
    );
  }

1 个答案:

答案 0 :(得分:1)

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


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

<manifest ...
  <application ...
    <meta-data android:name="com.google.android.geo.API_KEY"
               android:value="YOUR KEY HERE"/>

或应用程序委托(iOS):

#include "AppDelegate.h"
#include "GeneratedPluginRegistrant.h"
#import "GoogleMaps/GoogleMaps.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [GMSServices provideAPIKey:@"YOUR KEY HERE"];
  [GeneratedPluginRegistrant registerWithRegistry:self];
  return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
@end

在iOS上,您还必须使用键Info.plist和值io.flutter.embedded_views_previewYES添加一个布尔值。

然后,您可以将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; });
  }
}