|
- import 'dart:async';
- import 'dart:convert';
-
- import 'package:chat/data/constants.dart';
- import 'package:chat/generated/i18n.dart';
- import 'package:chat/map/location_result.dart';
- import 'package:chat/map/map.dart';
- import 'package:chat/r.dart';
- import 'package:chat/utils/screen_shot.dart';
- import 'package:flutter/material.dart';
- import 'package:google_maps_flutter/google_maps_flutter.dart';
- import 'package:http/http.dart' as http;
-
- class MapView extends StatefulWidget {
- MapView({
- Key key,
- this.locationResult,
- this.requiredGPS = true,
- });
-
- final LocationResult locationResult;
-
- final bool requiredGPS;
-
- @override
- MapViewState createState() => MapViewState();
- }
-
- class MapViewState extends State<MapView> {
- Completer<GoogleMapController> mapController = Completer();
-
- var mapKey = GlobalKey<MapPickerState>();
-
- String address;
-
- BitmapDescriptor _markerIcon;
-
- void moveToMyLocation(LatLng latLng) {
- mapKey.currentState.mapController.future.then((controller) {
- controller.animateCamera(
- CameraUpdate.newCameraPosition(
- CameraPosition(
- target: latLng,
- zoom: 18.0,
- ),
- ),
- );
- });
- }
-
- @override
- void initState() {
- super.initState();
- address = widget.locationResult.address;
-
- reverseGeocodeLatLng(widget.locationResult.latLng);
- }
-
- @override
- void dispose() {
- mapKey = null;
- super.dispose();
- }
-
- @override
- Widget build(BuildContext context) {
- _createMarkerImageFromAsset(context);
-
- return Scaffold(
- body: SafeArea(
- child: Center(
- child: Stack(
- children: <Widget>[
- GoogleMap(
- onMapCreated: (GoogleMapController controller) {
- mapController.complete(controller);
- },
- initialCameraPosition: CameraPosition(
- target: widget.locationResult.latLng,
- zoom: 18.0,
- ),
- markers: _createMarker(),
- myLocationEnabled: true,
- myLocationButtonEnabled: true),
- locationCard(),
- Positioned(
- left: 10,
- top: 10,
- child: IconButton(
- icon: Icon(Icons.arrow_back_ios),
- onPressed: () {
- Navigator.pop(context);
- },
- ),
- )
- ],
- ))));
- }
-
- Future reverseGeocodeLatLng(LatLng latLng) async {
- /*
- var placeMarks = await Geolocator()
- .placemarkFromCoordinates(latLng.latitude, latLng.longitude);
-
- if (placeMarks == null) {
- return;
- }
- var place = placeMarks.first;
-
-
- setState(() {
- address = place.name;
- });
- */
-
- var response = await http.get(
- "https://maps.googleapis.com/maps/api/geocode/json?latlng=${latLng.latitude},${latLng.longitude}"
- "&key=$googleMapApiKey");
-
- if (response.statusCode == 200) {
- Map<String, dynamic> responseJson = jsonDecode(response.body);
-
- setState(() {
- address = responseJson['results'][0]['formatted_address'];
- });
- }
- }
-
- Set<Marker> _createMarker() {
- return <Marker>[
- Marker(
- markerId: MarkerId("marker_1"),
- position: widget.locationResult.latLng,
- icon: _markerIcon,
- ),
- ].toSet();
- }
-
- Future<void> _createMarkerImageFromAsset(BuildContext context) async {
- if (_markerIcon == null) {
- final ImageConfiguration imageConfiguration =
- createLocalImageConfiguration(context);
- BitmapDescriptor.fromAssetImage(
- imageConfiguration, R.assetsImagesDefaultNorAvatar)
- .then(_updateBitmap);
- }
- }
-
- void _updateBitmap(BitmapDescriptor bitmap) {
- setState(() {
- // _markerIcon = bitmap;
- _markerIcon =
- BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueRed);
- });
- }
-
- Widget locationCard() {
- return Align(
- alignment: Alignment.bottomCenter,
- child: Padding(
- padding: const EdgeInsets.fromLTRB(8, 8, 8, 24),
- child: Card(
- child: Padding(
- padding: const EdgeInsets.all(8),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: <Widget>[
- Flexible(
- flex: 20,
- child: Column(
- mainAxisSize: MainAxisSize.min,
- children: [
- Text(
- address ?? '',
- textScaleFactor: 1.0,
- style: TextStyle(
- fontSize: 18,
- ),
- ),
- ],
- )),
- Spacer(),
- FloatingActionButton(
- onPressed: () async {
- _showMapApps();
- },
- child: Icon(Icons.navigation, color: Colors.white),
- ),
- ],
- )),
- ),
- ),
- );
- }
-
- getMapName(String mapType) {
- var name;
- switch (mapType) {
- case 'minimap':
- name = I18n.of(context).amap;
- break;
- case 'baidu':
- name = I18n.of(context).baidumap;
- break;
- case 'google':
- name = I18n.of(context).googlemap;
- break;
- case 'apple':
- name = I18n.of(context).applemap;
-
- break;
- default:
- }
-
- return name;
- }
-
- _showMapApps() async {
- var mapList = await ScreenShot.getOtherMapAppList();
-
- print('可用地图' + mapList.toString());
-
- showModalBottomSheet(
- context: context,
- builder: (BuildContext context) {
- return Container(
- color: Colors.transparent,
- child: Column(
- mainAxisSize: MainAxisSize.min,
- children: List<Widget>.generate(
- mapList.length,
- (int i) => Padding(
- padding: EdgeInsets.only(bottom: 2),
- child: Container(
- width: double.infinity,
- color: Colors.white,
- child: FlatButton(
- child: Text(getMapName(mapList[i]), textScaleFactor: 1.0,),
- onPressed: () {
- print('跳转到其他地图');
- ScreenShot.openMapForOth(
- mapList[i], widget.locationResult);
- }))))),
- );
- },
- );
- }
- }
|