|
add google map to flutter app |
To embed Google Maps in a Flutter app, you need to add the google_maps_flutter package to your project.
Here's an example code that displays a Google Map in a Flutter app:
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
class MapScreen extends StatefulWidget {
@override
_MapScreenState createState() => _MapScreenState();
}
class _MapScreenState extends State<MapScreen> {
GoogleMapController _controller;
final LatLng _center = const LatLng(37.7749, -122.4194);
void _onMapCreated(GoogleMapController controller) {
_controller = controller;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Google Maps'),
),
body: GoogleMap(
onMapCreated: _onMapCreated,
initialCameraPosition: CameraPosition(
target: _center,
zoom: 11.0,
),
),
);
}
}
In this example, we have defined a MapScreen widget that contains a GoogleMap widget. We have also defined an _onMapCreated method that sets the GoogleMapController variable _controller when the map is created.
To use this MapScreen widget in your app, simply add it to your widget tree. For example:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My App',
home: MapScreen(),
);
}
}
This will display the MapScreen widget in your app, which contains the embedded Google Map.