Last active
November 20, 2020 17:33
-
-
Save dariadobsai/7a54f43c88c2fc0756f55c661b40aa12 to your computer and use it in GitHub Desktop.
MapBox in Flutter Part 1
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
class HomePage extends StatefulWidget { | |
@override | |
_HomePageState createState() => _HomePageState(); | |
} | |
class _HomePageState extends State<HomePage> { | |
MapController mapController = MapController(); | |
UserLocationOptions userLocationOptions; | |
List<Marker> markers = []; | |
@override | |
Widget build(BuildContext context) { | |
userLocationOptions = UserLocationOptions( | |
context: context, | |
mapController: mapController, | |
zoomToCurrentLocationOnLoad: false, | |
updateMapLocationOnPositionChange: false, | |
showMoveToCurrentLocationFloatingActionButton: false, | |
markers: markers, | |
); | |
return Scaffold( | |
body: FlutterMap( | |
options: MapOptions( | |
onLongPress: addPin, | |
center: LatLng(51.5074, 0.1278), | |
zoom: 16.0, | |
minZoom: 10, | |
plugins: [ | |
UserLocationPlugin(), | |
], | |
), | |
layers: [ | |
new TileLayerOptions( | |
urlTemplate: | |
"https://api.mapbox.com/styles/v1/{user}/{style}/tiles/256/{z}/{x}/{y}@2x?access_token={accessToken}", | |
additionalOptions: { | |
'accessToken': | |
'YOUR accessToken', | |
}, | |
), | |
MarkerLayerOptions( | |
markers: markers, | |
), | |
userLocationOptions, | |
], | |
mapController: mapController, | |
), | |
); | |
} | |
addPin(LatLng latlng) { | |
setState(() { | |
markers.add(Marker( | |
width: 30.0, | |
height: 30.0, | |
point: latlng, | |
builder: (ctx) => Container( | |
child: Image.asset('assets/pin.png'), | |
), | |
)); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment