Created
July 2, 2020 06:16
-
-
Save FilledStacks/d2e96959c5fd8eb5374796d08cdc1fdc to your computer and use it in GitHub Desktop.
This file contains hidden or 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/foundation.dart'; | |
import 'package:flutter/gestures.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:google_maps_flutter/google_maps_flutter.dart'; | |
class MapTest extends StatefulWidget { | |
const MapTest({Key key}) : super(key: key); | |
@override | |
_MapTestState createState() => _MapTestState(); | |
} | |
class _MapTestState extends State<MapTest> { | |
GoogleMapController _controller; | |
bool swapBounds = true; | |
var userLatLong = LatLng(26.462084, -80.07101); | |
var merchantLatLong = LatLng(26.4846112, -80.064216); | |
LatLngBounds getCurrentBounds(LatLng position1, LatLng position2) { | |
LatLngBounds bounds; | |
try { | |
bounds = LatLngBounds( | |
northeast: position1, | |
southwest: position2, | |
); | |
} catch (_) { | |
bounds = LatLngBounds( | |
northeast: position2, | |
southwest: position1, | |
); | |
} | |
return bounds; | |
} | |
@override | |
Widget build(BuildContext context) { | |
var userCameraPosition = CameraPosition( | |
target: userLatLong, | |
zoom: 16.151926040649414, | |
); | |
return Scaffold( | |
body: GoogleMap( | |
onMapCreated: (controller) { | |
if (_controller == null) { | |
_controller = controller; | |
} | |
}, | |
mapType: MapType.terrain, | |
initialCameraPosition: userCameraPosition, | |
markers: Set.from([ | |
Marker( | |
markerId: MarkerId('Your location'), | |
position: userLatLong, | |
), | |
Marker( | |
markerId: MarkerId('Merchant Location'), | |
position: merchantLatLong, | |
), | |
]), | |
cameraTargetBounds: | |
CameraTargetBounds(getCurrentBounds(userLatLong, merchantLatLong)), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () { | |
setState(() { | |
swapBounds = !swapBounds; | |
userLatLong = LatLng(26.462084, -80.07101); | |
merchantLatLong = swapBounds | |
? LatLng(26.4636579, -80.07231829999999) | |
: LatLng(26.4846112, -80.064216); | |
updateMapToBounds(getCurrentBounds(userLatLong, merchantLatLong)); | |
}); | |
}, | |
), | |
); | |
} | |
Future updateMapToBounds(LatLngBounds bounds) async { | |
await Future.delayed(Duration(milliseconds: 50)); | |
_controller.animateCamera( | |
CameraUpdate.newLatLngBounds(bounds, 40), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment