-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.dart
192 lines (163 loc) · 5.42 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import 'package:flutter/material.dart';
import 'package:map_view/camera_position.dart';
import 'package:map_view/location.dart';
import 'package:map_view/map_options.dart';
import 'package:map_view/map_view.dart';
import 'package:location/location.dart';
import 'package:flutter/services.dart';
import 'package:map_view/marker.dart';
import 'package:map_view/toolbar_action.dart';
var api_key = "AIzaSyDrHKl8IxB4cGXIoELXQOzzZwiH1xtsRf4";
void main() {
MapView.setApiKey(api_key);
runApp(new MaterialApp(
debugShowCheckedModeBanner: false,
home: new MapPage(),
));
// runApp(new MyApp());
}
class MapPage extends StatefulWidget {
@override
_MapPageState createState() => new _MapPageState();
}
class _MapPageState extends State<MapPage> {
MapView mapView = new MapView();
CameraPosition cameraPosition=new CameraPosition(Location(24.8933854, 67.0859034), 2.0);
var staticMapProvider = new StaticMapProvider(api_key);
Uri staticMapUri;
LocationNew _location = new LocationNew();
String error;
Map<String, double> _startLocation;
List<Marker> markers = <Marker>[
new Marker("1", "Faisal Cantonment"
, 24.895314, 67.136886,
color: Colors.amber),
new Marker("2", "National Highway", 24.886618, 67.124282,
color: Colors.purple),
];
showMap() async {
// await getCurrentLocation();
print("show map tapped $_startLocation");
mapView.show(
new MapOptions(
mapViewType: MapViewType.normal,
showUserLocation: true,
showMyLocationButton: true,
showCompassButton: true,
// initialCameraPosition: new CameraPosition(
// new Location(45.526607443935724, -122.66731660813093), 15.0),
hideToolbar: false,
title: "Recently Visited"),
toolbarActions: [new ToolbarAction("Close", 1)]);
// mapView.show(new MapOptions(
// mapViewType: MapViewType.normal,
// initialCameraPosition: new CameraPosition(new Location(_startLocation["latitude"], _startLocation["longitude"]), 15.0),
// //cameraPosition,
// // new CameraPosition(new Location(28.420035, 77.337628), 15.0),
// showUserLocation: true,
// title: "Recent Location"));
//
// mapView.setMarkers(markers);
mapView.zoomToFit(padding: 100);
mapView.onMapTapped.listen((_) {
setState(() {
mapView.setMarkers(markers);
mapView.zoomToFit(padding: 100);
});
});
}
@override
void initState() {
// TODO: implement initState
super.initState();
cameraPosition =
new CameraPosition(new Location(24.8933854, 67.0859034), 2.0);
staticMapUri = staticMapProvider.getStaticUri(
new Location(24.8933854, 67.0859034), 12,
height: 400, width: 900, mapType: StaticMapViewType.roadmap);
getLocation();
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(
title: Text("Flutter maps"),
),
body: new Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
new Container(
height: 300.0,
child: new Stack(
children: <Widget>[
new Center(
child: Container(
child: new Text(
"Map should show here",
textAlign: TextAlign.center,
),
padding: const EdgeInsets.all(20.0),
),
),
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),
),
),
new Container(
padding: new EdgeInsets.only(top: 25.0),
child: new Text(
"Camera Position: \n\nLat: ${cameraPosition.center
.latitude}\n\nLng:${cameraPosition.center
.longitude}\n\nZoom: ${cameraPosition.zoom}"),
),
],
),
);
}
void getLocation() async {
print("getting location");
Map<String, double> location;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
location = await _location.getLocation;
error = null;
} on PlatformException catch (e) {
print("exception getting location");
if (e.code == 'PERMISSION_DENIED') {
error = 'Permission denied';
} else if (e.code == 'PERMISSION_DENIED_NEVER_ASK') {
error =
'Permission denied - please ask the user to enable it from the app settings';
}
location = null;
}
setState(() {
print("location is $location");
_startLocation = location;
if (_startLocation != null) {
cameraPosition =
new CameraPosition(new Location(
_startLocation["latitude"], _startLocation["longitude"]), 2.0);
staticMapUri = staticMapProvider.getStaticUri(
new Location(_startLocation["latitude"], _startLocation["longitude"]), 12,
height: 400, width: 900, mapType: StaticMapViewType.roadmap);
}
else
{
print("_startLocation is null");
}
});
}
}