Last active
December 19, 2018 16:02
-
-
Save kenzieschmoll/2578ca70ff059ee8f8335e9626e8c79f to your computer and use it in GitHub Desktop.
maps sample - final main.dart code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:google_maps_flutter/google_maps_flutter.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatefulWidget { | |
@override | |
_MyAppState createState() => _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
GoogleMapController mapController; | |
MapType _currentMapType = MapType.normal; | |
final LatLng _center = const LatLng(45.521563, -122.677433); | |
void _onMapCreated(GoogleMapController controller) { | |
mapController = controller; | |
} | |
void _onMapTypeButtonPressed() { | |
if (_currentMapType == MapType.normal) { | |
mapController.updateMapOptions( | |
GoogleMapOptions(mapType: MapType.satellite), | |
); | |
_currentMapType = MapType.satellite; | |
} else { | |
mapController.updateMapOptions( | |
GoogleMapOptions(mapType: MapType.normal), | |
); | |
_currentMapType = MapType.normal; | |
} | |
} | |
void _onAddMarkerButtonPressed() { | |
mapController.addMarker( | |
MarkerOptions( | |
position: LatLng( | |
mapController.cameraPosition.target.latitude, | |
mapController.cameraPosition.target.longitude, | |
), | |
infoWindowText: InfoWindowText('Random Place', '5 Star Rating'), | |
icon: BitmapDescriptor.defaultMarker, | |
), | |
); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
appBar: AppBar( | |
title: Text('Maps Sample App'), | |
backgroundColor: Colors.green[700], | |
), | |
body: Stack( | |
children: <Widget>[ | |
GoogleMap( | |
onMapCreated: _onMapCreated, | |
options: GoogleMapOptions( | |
trackCameraPosition: true, | |
cameraPosition: CameraPosition( | |
target: _center, | |
zoom: 11.0, | |
), | |
), | |
), | |
Padding( | |
padding: const EdgeInsets.all(16.0), | |
child: Align( | |
alignment: Alignment.topRight, | |
child: Column( | |
children: <Widget>[ | |
FloatingActionButton( | |
onPressed: _onMapTypeButtonPressed, | |
materialTapTargetSize: MaterialTapTargetSize.padded, | |
backgroundColor: Colors.green, | |
child: const Icon(Icons.map, size: 36.0), | |
), | |
const SizedBox(height: 16.0), | |
FloatingActionButton( | |
onPressed: _onAddMarkerButtonPressed, | |
materialTapTargetSize: MaterialTapTargetSize.padded, | |
backgroundColor: Colors.green, | |
child: const Icon(Icons.add_location, size: 36.0), | |
), | |
], | |
), | |
), | |
), | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment