import 'dart:convert'; import 'package:amap_location_example/map_util.dart'; import 'package:flutter/material.dart'; import 'package:amap_location/amap_export.dart'; import 'dart:io'; class MapViewPage extends StatefulWidget { @override State createState() { return _MapViewPageState(); } } class _MapViewPageState extends State with WidgetsBindingObserver { final AmapViewController controller = AmapViewController(); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text("Map View")), body: SizedBox( height: double.maxFinite, width: double.maxFinite, child: AmapView( controller: controller, onViewCreated: _onPlatformCreate, ), ), ); } @override void initState() { super.initState(); WidgetsBinding.instance.addObserver(this); } @override void dispose() { WidgetsBinding.instance.removeObserver(this); controller.dispose(); super.dispose(); } @override void didChangeAppLifecycleState(AppLifecycleState state) { super.didChangeAppLifecycleState(state); if (state == AppLifecycleState.paused) { controller.onPauseMapView(); } else if (state == AppLifecycleState.resumed) { controller.onResumeMapView(); } } void initCityData() async { // kingWay 的环境 var httpClient = new HttpClient(); HttpClientRequest request = await httpClient .getUrl(Uri.http("47.103.219.158:31103", "/api/v1/school_map/all_addr")); var response = await request.close(); var responseBody = await response.transform(Utf8Decoder()).join(); List data = jsonDecode(responseBody)['data'].cast(); print(data.toString()); controller.setMapMarkers(data.map((f) { var coordinate = f['coordinate'].toString().split(","); return AmapMarker( double.parse(coordinate[1]), double.parse(coordinate[0]), {"title": f["schoolname"], "content": f["address"], "tel": f["tel"]}); }).toList()); } void _onPlatformCreate() { print("on map view create"); controller.onCreateMapView(); initCityData(); controller.guideStream.listen((m) { showModalBottomSheet( context: context, builder: (context) { return Container( color: Colors.white, height: Platform.isIOS ? 158 + 50 : 158, margin: EdgeInsets.only( bottom: MediaQuery.of(context).padding.bottom, ), child: Column( mainAxisSize: MainAxisSize.max, mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ Offstage( offstage: !Platform.isIOS, child: Column( children: [ Container( height: 50, child: Material( color: Colors.white, child: InkWell( child: Center( child: Text( "苹果地图", style: TextStyle( fontSize: 15, ), ), ), onTap: () => _goThirdPartMapApp(context, MapType.Apple, m), ), ), ), Divider( height: 1, ), ], ), ), Container( height: 50, child: Material( color: Colors.white, child: InkWell( child: Center( child: Text( "高德地图", style: TextStyle( fontSize: 15, ), ), ), onTap: () => _goThirdPartMapApp(context, MapType.AMap, m), ), ), ), Divider( height: 1, ), Container( height: 50, child: Material( color: Colors.white, child: InkWell( child: Center( child: Text( "百度地图", style: TextStyle( fontSize: 15, ), ), ), onTap: () => _goThirdPartMapApp(context, MapType.Baidu, m), ), ), ), Divider( height: 1, ), Container( height: 50, child: Material( color: Colors.white, child: InkWell( child: Center( child: Text( "取消", style: TextStyle( fontSize: 15, ), ), ), onTap: () { Navigator.of(context).pop(); }, ), ), ), ], ), ); }); }); } void _goThirdPartMapApp(BuildContext context, MapType type, dynamic data) async { // close bottom sheet Navigator.of(context).pop(); bool success; if (type == MapType.AMap) { success = await MapUtil.goAMap(data['lon'], data['lat']); } else if (type == MapType.Baidu) { success = await MapUtil.goBaiduMap(data['lon'], data['lat']); } else if (type == MapType.Apple) { success = await MapUtil.goAppleMap(data['lon'], data['lat']); } print("jump ${success ? "successful" : "failed"}"); } } enum MapType { Apple, Baidu, AMap }