| // Copyright 2018 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| import 'package:flutter/material.dart'; |
| import 'package:google_maps_flutter/google_maps_flutter.dart'; |
| |
| import 'page.dart'; |
| |
| final LatLngBounds sydneyBounds = LatLngBounds( |
| southwest: const LatLng(-34.022631, 150.620685), |
| northeast: const LatLng(-33.571835, 151.325952), |
| ); |
| |
| class MapUiPage extends Page { |
| MapUiPage() : super(const Icon(Icons.map), 'User interface'); |
| |
| @override |
| Widget build(BuildContext context) { |
| return const MapUiBody(); |
| } |
| } |
| |
| class MapUiBody extends StatefulWidget { |
| const MapUiBody(); |
| |
| @override |
| State<StatefulWidget> createState() => MapUiBodyState(); |
| } |
| |
| class MapUiBodyState extends State<MapUiBody> { |
| MapUiBodyState(); |
| |
| static final CameraPosition _kInitialPosition = const CameraPosition( |
| target: LatLng(-33.852, 151.211), |
| zoom: 11.0, |
| ); |
| |
| GoogleMapController mapController; |
| CameraPosition _position = _kInitialPosition; |
| bool _isMoving = false; |
| bool _compassEnabled = true; |
| CameraTargetBounds _cameraTargetBounds = CameraTargetBounds.unbounded; |
| MinMaxZoomPreference _minMaxZoomPreference = MinMaxZoomPreference.unbounded; |
| MapType _mapType = MapType.normal; |
| bool _rotateGesturesEnabled = true; |
| bool _scrollGesturesEnabled = true; |
| bool _tiltGesturesEnabled = true; |
| bool _zoomGesturesEnabled = true; |
| bool _myLocationEnabled = true; |
| |
| @override |
| void initState() { |
| super.initState(); |
| } |
| |
| void _onMapChanged() { |
| setState(() { |
| _extractMapInfo(); |
| }); |
| } |
| |
| void _extractMapInfo() { |
| _position = mapController.cameraPosition; |
| _isMoving = mapController.isCameraMoving; |
| } |
| |
| @override |
| void dispose() { |
| mapController.removeListener(_onMapChanged); |
| super.dispose(); |
| } |
| |
| Widget _compassToggler() { |
| return FlatButton( |
| child: Text('${_compassEnabled ? 'disable' : 'enable'} compass'), |
| onPressed: () { |
| setState(() { |
| _compassEnabled = !_compassEnabled; |
| }); |
| }, |
| ); |
| } |
| |
| Widget _latLngBoundsToggler() { |
| return FlatButton( |
| child: Text( |
| _cameraTargetBounds.bounds == null |
| ? 'bound camera target' |
| : 'release camera target', |
| ), |
| onPressed: () { |
| setState(() { |
| _cameraTargetBounds = _cameraTargetBounds.bounds == null |
| ? CameraTargetBounds(sydneyBounds) |
| : CameraTargetBounds.unbounded; |
| }); |
| }, |
| ); |
| } |
| |
| Widget _zoomBoundsToggler() { |
| return FlatButton( |
| child: Text(_minMaxZoomPreference.minZoom == null |
| ? 'bound zoom' |
| : 'release zoom'), |
| onPressed: () { |
| setState(() { |
| _minMaxZoomPreference = _minMaxZoomPreference.minZoom == null |
| ? const MinMaxZoomPreference(12.0, 16.0) |
| : MinMaxZoomPreference.unbounded; |
| }); |
| }, |
| ); |
| } |
| |
| Widget _mapTypeCycler() { |
| final MapType nextType = |
| MapType.values[(_mapType.index + 1) % MapType.values.length]; |
| return FlatButton( |
| child: Text('change map type to $nextType'), |
| onPressed: () { |
| setState(() { |
| _mapType = nextType; |
| }); |
| }, |
| ); |
| } |
| |
| Widget _rotateToggler() { |
| return FlatButton( |
| child: Text('${_rotateGesturesEnabled ? 'disable' : 'enable'} rotate'), |
| onPressed: () { |
| setState(() { |
| _rotateGesturesEnabled = !_rotateGesturesEnabled; |
| }); |
| }, |
| ); |
| } |
| |
| Widget _scrollToggler() { |
| return FlatButton( |
| child: Text('${_scrollGesturesEnabled ? 'disable' : 'enable'} scroll'), |
| onPressed: () { |
| setState(() { |
| _scrollGesturesEnabled = !_scrollGesturesEnabled; |
| }); |
| }, |
| ); |
| } |
| |
| Widget _tiltToggler() { |
| return FlatButton( |
| child: Text('${_tiltGesturesEnabled ? 'disable' : 'enable'} tilt'), |
| onPressed: () { |
| setState(() { |
| _tiltGesturesEnabled = !_tiltGesturesEnabled; |
| }); |
| }, |
| ); |
| } |
| |
| Widget _zoomToggler() { |
| return FlatButton( |
| child: Text('${_zoomGesturesEnabled ? 'disable' : 'enable'} zoom'), |
| onPressed: () { |
| setState(() { |
| _zoomGesturesEnabled = !_zoomGesturesEnabled; |
| }); |
| }, |
| ); |
| } |
| |
| Widget _myLocationToggler() { |
| return FlatButton( |
| child: Text('${_myLocationEnabled ? 'disable' : 'enable'} my location'), |
| onPressed: () { |
| setState(() { |
| _myLocationEnabled = !_myLocationEnabled; |
| }); |
| }, |
| ); |
| } |
| |
| @override |
| Widget build(BuildContext context) { |
| final GoogleMap googleMap = GoogleMap( |
| onMapCreated: onMapCreated, |
| initialCameraPosition: _kInitialPosition, |
| trackCameraPosition: true, |
| compassEnabled: _compassEnabled, |
| cameraTargetBounds: _cameraTargetBounds, |
| minMaxZoomPreference: _minMaxZoomPreference, |
| mapType: _mapType, |
| rotateGesturesEnabled: _rotateGesturesEnabled, |
| scrollGesturesEnabled: _scrollGesturesEnabled, |
| tiltGesturesEnabled: _tiltGesturesEnabled, |
| zoomGesturesEnabled: _zoomGesturesEnabled, |
| myLocationEnabled: _myLocationEnabled, |
| ); |
| |
| final List<Widget> columnChildren = <Widget>[ |
| Padding( |
| padding: const EdgeInsets.all(10.0), |
| child: Center( |
| child: SizedBox( |
| width: 300.0, |
| height: 200.0, |
| child: googleMap, |
| ), |
| ), |
| ), |
| ]; |
| |
| if (mapController != null) { |
| columnChildren.add( |
| Expanded( |
| child: ListView( |
| children: <Widget>[ |
| Text('camera bearing: ${_position.bearing}'), |
| Text( |
| 'camera target: ${_position.target.latitude.toStringAsFixed(4)},' |
| '${_position.target.longitude.toStringAsFixed(4)}'), |
| Text('camera zoom: ${_position.zoom}'), |
| Text('camera tilt: ${_position.tilt}'), |
| Text(_isMoving ? '(Camera moving)' : '(Camera idle)'), |
| _compassToggler(), |
| _latLngBoundsToggler(), |
| _mapTypeCycler(), |
| _zoomBoundsToggler(), |
| _rotateToggler(), |
| _scrollToggler(), |
| _tiltToggler(), |
| _zoomToggler(), |
| _myLocationToggler(), |
| ], |
| ), |
| ), |
| ); |
| } |
| return Column( |
| mainAxisAlignment: MainAxisAlignment.start, |
| crossAxisAlignment: CrossAxisAlignment.stretch, |
| children: columnChildren, |
| ); |
| } |
| |
| void onMapCreated(GoogleMapController controller) { |
| mapController = controller; |
| mapController.addListener(_onMapChanged); |
| _extractMapInfo(); |
| setState(() {}); |
| } |
| } |