Skip to content

Instantly share code, notes, and snippets.

@hoc081098
Created July 19, 2018 14:03
Show Gist options
  • Save hoc081098/ab62595020b439f4bb3d0f457309ac22 to your computer and use it in GitHub Desktop.
Save hoc081098/ab62595020b439f4bb3d0f457309ac22 to your computer and use it in GitHub Desktop.
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong/latlong.dart';
import 'package:location/location.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var mapController = new MapController();
var point = new LatLng(18.0, 106.0);
StreamSubscription<Map<String, double>> subscription;
@override
void initState() {
super.initState();
subscription = new Location().onLocationChanged.listen((location) {
point = new LatLng(location['latitude'], location['longitude']);
mapController.move(point, 10.0);
setState(() {});
});
}
@override
void dispose() {
subscription.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: Text('Demo map'),
),
body: new FlutterMap(
mapController: mapController,
options: new MapOptions(
center: point,
zoom: 5.0,
),
layers: <LayerOptions>[
new TileLayerOptions(
urlTemplate: 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
subdomains: ['a', 'b', 'c'],
),
new MarkerLayerOptions(
markers: <Marker>[
new Marker(
width: 80.0,
height: 80.0,
point: point,
builder: (BuildContext context) {
return new Icon(
Icons.location_on,
color: Colors.red,
);
})
],
)
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment