This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.9k
/
map_coordinates.dart
100 lines (86 loc) · 2.98 KB
/
map_coordinates.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
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// ignore_for_file: public_member_api_docs
import 'package:flutter/material.dart';
import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart';
import 'example_google_map.dart';
import 'page.dart';
const CameraPosition _kInitialPosition =
CameraPosition(target: LatLng(-33.852, 151.211), zoom: 11.0);
class MapCoordinatesPage extends GoogleMapExampleAppPage {
const MapCoordinatesPage({Key? key})
: super(const Icon(Icons.map), 'Map coordinates', key: key);
@override
Widget build(BuildContext context) {
return const _MapCoordinatesBody();
}
}
class _MapCoordinatesBody extends StatefulWidget {
const _MapCoordinatesBody();
@override
State<StatefulWidget> createState() => _MapCoordinatesBodyState();
}
class _MapCoordinatesBodyState extends State<_MapCoordinatesBody> {
_MapCoordinatesBodyState();
ExampleGoogleMapController? mapController;
LatLngBounds _visibleRegion = LatLngBounds(
southwest: const LatLng(0, 0),
northeast: const LatLng(0, 0),
);
@override
Widget build(BuildContext context) {
final ExampleGoogleMap googleMap = ExampleGoogleMap(
onMapCreated: onMapCreated,
initialCameraPosition: _kInitialPosition,
onCameraIdle:
_updateVisibleRegion, // https://github.com/flutter/flutter/issues/54758
);
return NotificationListener<ScrollNotification>(
onNotification: (ScrollNotification scrollState) {
_updateVisibleRegion();
return true;
},
child: ListView(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(10.0),
child: Center(
child: SizedBox(
width: 300.0,
height: 200.0,
child: googleMap,
),
),
),
if (mapController != null)
Center(
child: Text('VisibleRegion:'
'\nnortheast: ${_visibleRegion.northeast},'
'\nsouthwest: ${_visibleRegion.southwest}'),
),
// Add a block at the bottom of this list to allow validation that the visible region of the map
// does not change when scrolled under the safe view on iOS.
// https://github.com/flutter/flutter/issues/107913
const SizedBox(
width: 300,
height: 1000,
),
],
),
);
}
Future<void> onMapCreated(ExampleGoogleMapController controller) async {
final LatLngBounds visibleRegion = await controller.getVisibleRegion();
setState(() {
mapController = controller;
_visibleRegion = visibleRegion;
});
}
Future<void> _updateVisibleRegion() async {
final LatLngBounds visibleRegion = await mapController!.getVisibleRegion();
setState(() {
_visibleRegion = visibleRegion;
});
}
}