| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- 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<StatefulWidget> createState() {
- return _MapViewPageState();
- }
- }
- class _MapViewPageState extends State<MapViewPage> with WidgetsBindingObserver {
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(title: Text("Map View")),
- body: SizedBox(
- height: double.maxFinite,
- width: double.maxFinite,
- child: AmapLocation.instance.buildMapView(_onPlatformCreate),
- ),
- );
- }
- @override
- void initState() {
- super.initState();
- WidgetsBinding.instance.addObserver(this);
- }
- @override
- void dispose() {
- WidgetsBinding.instance.removeObserver(this);
- AmapLocation.instance.disposedMapView();
- super.dispose();
- }
- @override
- void didChangeAppLifecycleState(AppLifecycleState state) {
- super.didChangeAppLifecycleState(state);
- if (state == AppLifecycleState.paused) {
- AmapLocation.instance.onPauseMapView();
- } else if (state == AppLifecycleState.resumed) {
- AmapLocation.instance.onResumeMapView();
- }
- }
- void initCityData() async {
- // kingWay 的环境
- var httpClient = new HttpClient();
- HttpClientRequest request = await httpClient
- .getUrl(Uri.http("172.16.11.77:8086", "/api/v1/school_map/all_addr"));
- var response = await request.close();
- var responseBody = await response.transform(Utf8Decoder()).join();
- List<Map> data = jsonDecode(responseBody)['data'].cast<Map>();
- print(data.toString());
- AmapLocation.instance.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");
- AmapLocation.instance.onCreateMapView();
- initCityData();
- AmapLocation.instance.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: <Widget>[
- Offstage(
- offstage: !Platform.isIOS,
- child: Column(
- children: <Widget>[
- 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
- }
|