173 lines
5.1 KiB
Dart
173 lines
5.1 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:map_view/map_view.dart';
|
|
import 'globals.dart' as globals;
|
|
|
|
class GenericMap extends StatefulWidget {
|
|
final String title;
|
|
final double latitude;
|
|
final double longitude;
|
|
|
|
GenericMap(
|
|
{@required this.title,
|
|
@required this.latitude,
|
|
@required this.longitude});
|
|
|
|
@override
|
|
_GenericMapState createState() => new _GenericMapState(
|
|
title: this.title, latitude: this.latitude, longitude: this.longitude);
|
|
}
|
|
|
|
class _GenericMapState extends State<GenericMap> {
|
|
MapView mapView = new MapView();
|
|
CameraPosition cameraPosition;
|
|
var compositeSubscription = new CompositeSubscription();
|
|
var staticMapProvider;
|
|
Uri staticMapUri;
|
|
final String title;
|
|
Location location;
|
|
|
|
_GenericMapState(
|
|
{@required this.title, @required latitude, @required longitude}) {
|
|
MapView.setApiKey(globals.gmapKey);
|
|
this.staticMapProvider = new StaticMapProvider(globals.gmapKey);
|
|
this.location = new Location(latitude, longitude);
|
|
}
|
|
|
|
List<Marker> _markers = <Marker>[
|
|
new Marker("1", "Work", 45.523970, -122.663081, color: Colors.blue),
|
|
new Marker("2", "Nossa Familia Coffee", 45.528788, -122.684633),
|
|
];
|
|
|
|
@override
|
|
initState() {
|
|
super.initState();
|
|
cameraPosition = new CameraPosition(location, 2.0);
|
|
staticMapUri = staticMapProvider.getStaticUri(location, 12,
|
|
width: 900, height: 400, mapType: StaticMapViewType.roadmap);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return new Scaffold(
|
|
appBar: new AppBar(
|
|
title: new Text(title),
|
|
),
|
|
body: new Column(
|
|
mainAxisAlignment: MainAxisAlignment.start,
|
|
children: <Widget>[
|
|
new Container(
|
|
height: 250.0,
|
|
child: new Stack(
|
|
children: <Widget>[
|
|
new InkWell(
|
|
child: new Center(
|
|
child: new Image.network(staticMapUri.toString()),
|
|
),
|
|
onTap: showMap,
|
|
)
|
|
],
|
|
),
|
|
),
|
|
new Container(
|
|
padding: new EdgeInsets.only(top: 10.0),
|
|
child: new Text(
|
|
"Tap the map to interact",
|
|
style: new TextStyle(fontWeight: FontWeight.bold),
|
|
),
|
|
),
|
|
]));
|
|
}
|
|
|
|
showMap() {
|
|
mapView.show(
|
|
new MapOptions(
|
|
mapViewType: MapViewType.normal,
|
|
showUserLocation: true,
|
|
initialCameraPosition: new CameraPosition(new Location(45.5235258, -122.6732493), 14.0),
|
|
title: this.title),
|
|
toolbarActions: [new ToolbarAction("Close", 1)]);
|
|
|
|
var sub = mapView.onMapReady.listen((_) {
|
|
mapView.setMarkers(_markers);
|
|
mapView.addMarker(new Marker("3", "10 Barrel", 45.5259467, -122.687747,
|
|
color: Colors.purple));
|
|
mapView.zoomToFit(padding: 100);
|
|
});
|
|
compositeSubscription.add(sub);
|
|
|
|
sub = mapView.onLocationUpdated
|
|
.listen((location) => print("Location updated $location"));
|
|
compositeSubscription.add(sub);
|
|
|
|
sub = mapView.onTouchAnnotation
|
|
.listen((annotation) => print("annotation tapped"));
|
|
compositeSubscription.add(sub);
|
|
|
|
sub = mapView.onMapTapped
|
|
.listen((location) => print("Touched location $location"));
|
|
compositeSubscription.add(sub);
|
|
|
|
sub = mapView.onCameraChanged.listen((cameraPosition) =>
|
|
this.setState(() => this.cameraPosition = cameraPosition));
|
|
compositeSubscription.add(sub);
|
|
|
|
sub = mapView.onToolbarAction.listen((id) {
|
|
if (id == 1) {
|
|
_handleDismiss();
|
|
}
|
|
});
|
|
compositeSubscription.add(sub);
|
|
|
|
sub = mapView.onInfoWindowTapped.listen((marker) {
|
|
print("Info Window Tapped for ${marker.title}");
|
|
});
|
|
compositeSubscription.add(sub);
|
|
}
|
|
|
|
_handleDismiss() async {
|
|
double zoomLevel = await mapView.zoomLevel;
|
|
Location centerLocation = await mapView.centerLocation;
|
|
List<Marker> visibleAnnotations = await mapView.visibleAnnotations;
|
|
print("Zoom Level: $zoomLevel");
|
|
print("Center: $centerLocation");
|
|
print("Visible Annotation Count: ${visibleAnnotations.length}");
|
|
var uri = await staticMapProvider.getImageUriFromMap(mapView,
|
|
width: 900, height: 400);
|
|
setState(() => staticMapUri = uri);
|
|
mapView.dismiss();
|
|
compositeSubscription.cancel();
|
|
}
|
|
}
|
|
|
|
class CompositeSubscription {
|
|
Set<StreamSubscription> _subscriptions = new Set();
|
|
|
|
void cancel() {
|
|
for (var n in this._subscriptions) {
|
|
n.cancel();
|
|
}
|
|
this._subscriptions = new Set();
|
|
}
|
|
|
|
void add(StreamSubscription subscription) {
|
|
this._subscriptions.add(subscription);
|
|
}
|
|
|
|
void addAll(Iterable<StreamSubscription> subs) {
|
|
_subscriptions.addAll(subs);
|
|
}
|
|
|
|
bool remove(StreamSubscription subscription) {
|
|
return this._subscriptions.remove(subscription);
|
|
}
|
|
|
|
bool contains(StreamSubscription subscription) {
|
|
return this._subscriptions.contains(subscription);
|
|
}
|
|
|
|
List<StreamSubscription> toList() {
|
|
return this._subscriptions.toList();
|
|
}
|
|
}
|