Last active
March 15, 2019 20:09
-
-
Save kenzieschmoll/2be477fb1910a5cf4ad766d8fb415d73 to your computer and use it in GitHub Desktop.
Maps Sample V2 - 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 'dart:async'; | |
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> { | |
Completer<GoogleMapController> _controller = Completer(); | |
static const LatLng _center = const LatLng(45.521563, -122.677433); | |
final Set<Marker> _markers = {}; | |
LatLng _lastMapPosition = _center; | |
MapType _currentMapType = MapType.normal; | |
void _onMapTypeButtonPressed() { | |
setState(() { | |
_currentMapType = _currentMapType == MapType.normal | |
? MapType.satellite | |
: MapType.normal; | |
}); | |
} | |
void _onAddMarkerButtonPressed() { | |
setState(() { | |
_markers.add(Marker( | |
// This marker id can be anything that uniquely identifies each marker. | |
markerId: MarkerId(_lastMapPosition.toString()), | |
position: _lastMapPosition, | |
infoWindow: InfoWindow( | |
title: 'Really cool place', | |
snippet: '5 Star Rating', | |
), | |
icon: BitmapDescriptor.defaultMarker, | |
)); | |
}); | |
} | |
void _onCameraMove(CameraPosition position) { | |
_lastMapPosition = position.target; | |
} | |
void _onMapCreated(GoogleMapController controller) { | |
_controller.complete(controller); | |
} | |
@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, | |
initialCameraPosition: CameraPosition( | |
target: _center, | |
zoom: 11.0, | |
), | |
mapType: _currentMapType, | |
markers: _markers, | |
onCameraMove: _onCameraMove, | |
), | |
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), | |
), | |
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