Hibok
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

251 satır
6.6 KiB

  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'package:chat/data/constants.dart';
  4. import 'package:chat/generated/i18n.dart';
  5. import 'package:chat/map/location_result.dart';
  6. import 'package:chat/map/map.dart';
  7. import 'package:chat/r.dart';
  8. import 'package:chat/utils/screen_shot.dart';
  9. import 'package:flutter/material.dart';
  10. import 'package:google_maps_flutter/google_maps_flutter.dart';
  11. import 'package:http/http.dart' as http;
  12. class MapView extends StatefulWidget {
  13. MapView({
  14. Key key,
  15. this.locationResult,
  16. this.requiredGPS = true,
  17. });
  18. final LocationResult locationResult;
  19. final bool requiredGPS;
  20. @override
  21. MapViewState createState() => MapViewState();
  22. }
  23. class MapViewState extends State<MapView> {
  24. Completer<GoogleMapController> mapController = Completer();
  25. var mapKey = GlobalKey<MapPickerState>();
  26. String address;
  27. BitmapDescriptor _markerIcon;
  28. void moveToMyLocation(LatLng latLng) {
  29. mapKey.currentState.mapController.future.then((controller) {
  30. controller.animateCamera(
  31. CameraUpdate.newCameraPosition(
  32. CameraPosition(
  33. target: latLng,
  34. zoom: 18.0,
  35. ),
  36. ),
  37. );
  38. });
  39. }
  40. @override
  41. void initState() {
  42. super.initState();
  43. address = widget.locationResult.address;
  44. reverseGeocodeLatLng(widget.locationResult.latLng);
  45. }
  46. @override
  47. void dispose() {
  48. mapKey = null;
  49. super.dispose();
  50. }
  51. @override
  52. Widget build(BuildContext context) {
  53. _createMarkerImageFromAsset(context);
  54. return Scaffold(
  55. body: SafeArea(
  56. child: Center(
  57. child: Stack(
  58. children: <Widget>[
  59. GoogleMap(
  60. onMapCreated: (GoogleMapController controller) {
  61. mapController.complete(controller);
  62. },
  63. initialCameraPosition: CameraPosition(
  64. target: widget.locationResult.latLng,
  65. zoom: 18.0,
  66. ),
  67. markers: _createMarker(),
  68. myLocationEnabled: true,
  69. myLocationButtonEnabled: true),
  70. locationCard(),
  71. Positioned(
  72. left: 10,
  73. top: 10,
  74. child: IconButton(
  75. icon: Icon(Icons.arrow_back_ios),
  76. onPressed: () {
  77. Navigator.pop(context);
  78. },
  79. ),
  80. )
  81. ],
  82. ))));
  83. }
  84. Future reverseGeocodeLatLng(LatLng latLng) async {
  85. /*
  86. var placeMarks = await Geolocator()
  87. .placemarkFromCoordinates(latLng.latitude, latLng.longitude);
  88. if (placeMarks == null) {
  89. return;
  90. }
  91. var place = placeMarks.first;
  92. setState(() {
  93. address = place.name;
  94. });
  95. */
  96. var response = await http.get(
  97. "https://maps.googleapis.com/maps/api/geocode/json?latlng=${latLng.latitude},${latLng.longitude}"
  98. "&key=$googleMapApiKey");
  99. if (response.statusCode == 200) {
  100. Map<String, dynamic> responseJson = jsonDecode(response.body);
  101. setState(() {
  102. address = responseJson['results'][0]['formatted_address'];
  103. });
  104. }
  105. }
  106. Set<Marker> _createMarker() {
  107. return <Marker>[
  108. Marker(
  109. markerId: MarkerId("marker_1"),
  110. position: widget.locationResult.latLng,
  111. icon: _markerIcon,
  112. ),
  113. ].toSet();
  114. }
  115. Future<void> _createMarkerImageFromAsset(BuildContext context) async {
  116. if (_markerIcon == null) {
  117. final ImageConfiguration imageConfiguration =
  118. createLocalImageConfiguration(context);
  119. BitmapDescriptor.fromAssetImage(
  120. imageConfiguration, R.assetsImagesDefaultNorAvatar)
  121. .then(_updateBitmap);
  122. }
  123. }
  124. void _updateBitmap(BitmapDescriptor bitmap) {
  125. setState(() {
  126. // _markerIcon = bitmap;
  127. _markerIcon =
  128. BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueRed);
  129. });
  130. }
  131. Widget locationCard() {
  132. return Align(
  133. alignment: Alignment.bottomCenter,
  134. child: Padding(
  135. padding: const EdgeInsets.fromLTRB(8, 8, 8, 24),
  136. child: Card(
  137. child: Padding(
  138. padding: const EdgeInsets.all(8),
  139. child: Row(
  140. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  141. children: <Widget>[
  142. Flexible(
  143. flex: 20,
  144. child: Column(
  145. mainAxisSize: MainAxisSize.min,
  146. children: [
  147. Text(
  148. address ?? '',
  149. style: TextStyle(
  150. fontSize: 18,
  151. ),
  152. ),
  153. ],
  154. )),
  155. Spacer(),
  156. FloatingActionButton(
  157. onPressed: () async {
  158. _showMapApps();
  159. },
  160. child: Icon(Icons.navigation, color: Colors.white),
  161. ),
  162. ],
  163. )),
  164. ),
  165. ),
  166. );
  167. }
  168. getMapName(String mapType) {
  169. var name;
  170. switch (mapType) {
  171. case 'minimap':
  172. name = I18n.of(context).amap;
  173. break;
  174. case 'baidu':
  175. name = I18n.of(context).baidumap;
  176. break;
  177. case 'google':
  178. name = I18n.of(context).googlemap;
  179. break;
  180. case 'apple':
  181. name = I18n.of(context).applemap;
  182. break;
  183. default:
  184. }
  185. return name;
  186. }
  187. _showMapApps() async {
  188. var mapList = await ScreenShot.getOtherMapAppList();
  189. print('可用地图' + mapList.toString());
  190. showModalBottomSheet(
  191. context: context,
  192. builder: (BuildContext context) {
  193. return Container(
  194. color: Colors.transparent,
  195. child: Column(
  196. mainAxisSize: MainAxisSize.min,
  197. children: List<Widget>.generate(
  198. mapList.length,
  199. (int i) => Padding(
  200. padding: EdgeInsets.only(bottom: 2),
  201. child: Container(
  202. width: double.infinity,
  203. color: Colors.white,
  204. child: FlatButton(
  205. child: Text(getMapName(mapList[i])),
  206. onPressed: () {
  207. print('跳转到其他地图');
  208. ScreenShot.openMapForOth(
  209. mapList[i], widget.locationResult);
  210. }))))),
  211. );
  212. },
  213. );
  214. }
  215. }