Last active
August 6, 2020 20:40
-
-
Save mulieriq/bec73c49e7fbb0249a7f62d096ad3bc4 to your computer and use it in GitHub Desktop.
Map With Custom Bitmaps /Icons
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:android_intent/android_intent.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter/cupertino.dart'; | |
import 'package:geolocator/geolocator.dart'; | |
import 'package:google_maps_flutter/google_maps_flutter.dart'; | |
import 'package:mccu_family/controllers/core/scoped.dart'; | |
import 'package:permission/permission.dart'; | |
import 'dart:async'; | |
import 'package:scoped_model/scoped_model.dart'; | |
class MapSample extends StatefulWidget { | |
final double lat; | |
final double long; | |
MapSample({this.lat, this.long}); | |
@override | |
State<MapSample> createState() => MapSampleState(); | |
} | |
class MapSampleState extends State<MapSample> { | |
Completer<GoogleMapController> _controller = Completer(); | |
List<Marker> markers = []; | |
BitmapDescriptor sourceIcon; | |
BitmapDescriptor destinationIcon; | |
Future reqPermission() async { | |
var permissions = await Permission.getPermissionsStatus( | |
[PermissionName.Location, PermissionName.Internet]); | |
if (permissions[0].permissionStatus == PermissionStatus.notAgain) { | |
await Permission.requestPermissions([PermissionName.Location]); | |
_checkGps(); | |
} else { | |
await Permission.requestPermissions([PermissionName.Location]); | |
_checkGps(); | |
} | |
} | |
var _geoLocator = Geolocator(); | |
var long; | |
var lat; | |
getLocation() async { | |
_geoLocator | |
.getPositionStream((LocationOptions( | |
accuracy: LocationAccuracy.high, distanceFilter: 1))) | |
.listen((position) async { | |
setState(() { | |
print(position); | |
lat = position.latitude; | |
long = position.longitude; | |
// Toast.show("${placemark[0].country}", context); | |
}); | |
}); | |
// Toast.show("${placemark[0].country}", context); | |
// StreamSubscription<Position> positionStream = | |
// _geoLocator.getPositionStream().listen((Position position) { | |
// setState(() { | |
// positionStream1 = position.latitude; | |
// position2 = position.longitude; | |
// speed = position.speed; | |
// alt = position.altitude; | |
// }); | |
// // Toast.show("${position.latitude.toString()}", context); | |
// }); | |
} | |
Future _checkGps() async { | |
if (!(await Geolocator().isLocationServiceEnabled())) { | |
if (Theme.of(context).platform == TargetPlatform.android) { | |
showDialog( | |
context: context, | |
builder: (BuildContext context) { | |
return CupertinoAlertDialog( | |
title: Text("Can't get Current location"), | |
content: | |
const Text('Please make sure you enable GPS and try again'), | |
actions: <Widget>[ | |
FlatButton( | |
child: Text('Ok'), | |
onPressed: () { | |
final AndroidIntent intent = AndroidIntent( | |
action: 'android.settings.LOCATION_SOURCE_SETTINGS'); | |
intent.launch(); | |
Navigator.of(context, rootNavigator: true).pop(); | |
}, | |
), | |
], | |
); | |
}, | |
); | |
} | |
} | |
} | |
void setSourceAndDestinationIcons() async { | |
sourceIcon = await BitmapDescriptor.fromAssetImage( | |
ImageConfiguration(devicePixelRatio: 0.5), | |
'lib/assets/driving_pin.png'); | |
destinationIcon = await BitmapDescriptor.fromAssetImage( | |
ImageConfiguration(devicePixelRatio: 0.5), | |
'lib/assets/destination_map_marker.png'); | |
} | |
@override | |
void initState() { | |
// TODO: implement initState | |
super.initState(); | |
//reqPermission(); | |
reqPermission().then((r) async { | |
return getLocation(); | |
// await AndroidIntent(action: 'action_location_source_settings') | |
// .launch() | |
// .then((r) { | |
// getLocation(); | |
// }); | |
}); | |
// widget.model.generateCorinates( | |
// data: widget.model.deviceTrips[widget.index].positions); | |
setSourceAndDestinationIcons(); | |
//setMapPins(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return ScopedModelDescendant( | |
builder: (BuildContext context, Widget child, Scoped model) { | |
return new Scaffold( | |
body: lat == null || long == null | |
? Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
crossAxisAlignment: CrossAxisAlignment.center, | |
children: <Widget>[ | |
Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: CircularProgressIndicator(), | |
), | |
Text( | |
"Loading Map Co-Ordinates ...", | |
style: TextStyle( | |
color: Colors.blueAccent, | |
fontWeight: FontWeight.w900), | |
) | |
], | |
), | |
) | |
: GoogleMap( | |
mapType: MapType.normal, | |
mapToolbarEnabled: true, | |
markers: Set.from(markers), | |
myLocationButtonEnabled: true, | |
zoomGesturesEnabled: true, | |
myLocationEnabled: true, | |
compassEnabled: true, | |
scrollGesturesEnabled: true, | |
initialCameraPosition: CameraPosition( | |
target: LatLng(lat, long), | |
zoom: 15.5, | |
), | |
polylines: model.polyline, | |
onMapCreated: (GoogleMapController controller) { | |
// controller.setMapStyle(Utils.mapStyles); | |
setMapPins(); | |
model.polyline.add(Polyline( | |
width: 5, | |
polylineId: PolylineId("Car Route"), | |
visible: true, | |
points: [ | |
LatLng(lat, long), | |
LatLng(widget.lat, widget.long) | |
], | |
color: Colors.blueAccent, | |
)); | |
_controller.complete(controller); | |
}, | |
), | |
// floatingActionButton: FloatingActionButton.extended( | |
// onPressed: _goToTheLake, | |
// label: Text('Go Last Location'), | |
// icon: Icon(Icons.directions_boat), | |
// ), | |
); | |
}, | |
); | |
} | |
void setMapPins() { | |
setState(() { | |
// source pin | |
markers.add(Marker( | |
onTap: () {}, | |
draggable: false, | |
markerId: MarkerId('sourcePin'), | |
position: LatLng(lat, long), | |
infoWindow: | |
InfoWindow(title: "Your Position", snippet: "Keep On Moving"), | |
icon: sourceIcon)); | |
// destination pin | |
markers.add(Marker( | |
markerId: MarkerId('destPin'), | |
position: LatLng(widget.lat, widget.long), | |
infoWindow: | |
InfoWindow(title: "Destination", snippet: "You Are There"), | |
icon: destinationIcon)); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment