import 'dart:io'; import 'package:dio/dio.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:geolocator/geolocator.dart'; import 'package:huixiang/data/address.dart'; import 'package:huixiang/generated/l10n.dart'; import 'package:huixiang/retrofit/retrofit_api.dart'; import 'package:huixiang/utils/location.dart'; import 'package:huixiang/utils/shared_preference.dart'; import 'package:huixiang/view_widget/request_permission.dart'; import 'package:permission_handler/permission_handler.dart' as PH; class AddressMapPage extends StatefulWidget { @override State createState() { return _AddressMapPage(); } } class _AddressMapPage extends State { // //加载自定义地图样式 // void _loadCustomData() async { // setState(() { // _mapController?.setCustomMapStyle('assets/map_style/chatian.sty', 0); // }); // } // LocationFlutterPlugin? aMapFlutterLocation; String city = "武汉市"; String keyWord = ""; // @override // void dispose() { // super.dispose(); // aMapFlutterLocation?.stopLocation(); // } ApiService? apiService; @override void initState() { super.initState(); apiService = ApiService( Dio(), context: context, token: SharedInstance.instance.token, ); LocationInstance.instance.startLocation(context, (Position? result) { print("event: $result"); if (result?.latitude != null && result?.longitude != null) { latLng = Position( latitude: result!.latitude, longitude: result.longitude, timestamp: DateTime.now(), accuracy: 0, altitude: 0, heading: 0, speed: 0, speedAccuracy: 0); LocationInstance.instance.getAddress(result.latitude, result.longitude)?.then((value) { if (value != null) { city = value.city ?? ""; } }); saveLatLng(result); searchPoi(latLng); } }); startLocation(); } Position? center; List
? poiList; searchPoi(Position? latLng) async { keyWord = textEditingController.text; print("keyWord: ${keyWord}"); var addressPoi = await apiService?.searchPoi( "${latLng?.latitude}", "${latLng?.longitude}", keyWord, 20, 1); //如果定位列表无法显示,可以考虑数据是否不在当前字段里面;例:原数据放在['pois']里面,后改成了数据放在['entrances']里面 List poi = addressPoi['pois'] ?? addressPoi['entrances']; poiList = poi .map((e) => Address.fromJson({ "address": e["address"] is List ? "" : e["address"], "area": e["adname"], "city": e["cityname"], "province": e["pname"], "latitude": e["location"].toString().split(",")[0], "longitude": e["location"].toString().split(",")[1], "cityInfo": e["name"], "id": "", "mid": "", "phone": "", "tag": "", "username": "", "isDefault": true, })) .toList(); setState(() {}); // BMFPoiNearbySearch bmfPoiNearbySearch = BMFPoiNearbySearch(); // bmfPoiNearbySearch.onGetPoiNearbySearchResult( // callback: (BMFPoiSearchResult result, BMFSearchErrorCode errorCode) { // // if ((DateTime.now().millisecondsSinceEpoch - time) > 2000) { // result.poiInfoList.map((e) => print("sssssssssss:${e.toMap()}")); // poiList = result.poiInfoList; // print("object112221: ${poiList.length}"); // setState(() { // time = DateTime.now().millisecondsSinceEpoch; // }); // // } // }); // bmfPoiNearbySearch.poiNearbySearch(BMFPoiNearbySearchOption( // keywords: [" "], // tags: ["小区","住宅","地标","建筑"], // radius: 100000, // location: latLng, // pageSize: 20, // pageIndex: 0, // isRadiusLimit: true, // )); } int time = 0; startLocation() async { if (!(await PH.Permission.locationWhenInUse.serviceStatus.isEnabled)) { enableLocation(); return; } if (await PH.Permission.location.isPermanentlyDenied) { requestDialog(); } else if (await PH.Permission.location.isGranted) { // S.current.zhengzaijiazai.loading; center = await Geolocator.getCurrentPosition(); Future.delayed(Duration(seconds: 6), () { SmartDialog.dismiss(); }); } // else if (await PH.Permission.location.) { // await PH.Permission.location.request(); // } else { if (Platform.isIOS) { //去设置中心 requestDialog(); } else { await PH.Permission.location.request(); } } } enableLocation() { showCupertinoDialog( context: context, builder: (context) { return RequestPermission( "assets/image/icon_permission_location_bg.webp", S.of(context).nindingweigongnengweikaiqi, S.of(context).weilexiangnintuijianfujindemendianxinxi, S.of(context).dakaidingwei, (result) async { if (result) { // final AndroidIntent intent = AndroidIntent( // action: 'action_location_source_settings', // package: "com.zsw.huixiang"); // await intent.launch(); // startLocation(); Geolocator.openLocationSettings(); } }, heightRatioWithWidth: 0.82, ); }, ); } requestDialog() { showCupertinoDialog( context: context, builder: (context) { return RequestPermission( "assets/image/icon_permission_location_bg.webp", S.of(context).nindingweiquanxianweiyunxu, S.of(context).weilexiangnintuijianfujindemendianxinxi, S.of(context).kaiqiquanxian, (result) async { if (result) { await PH.openAppSettings(); if (await PH.Permission.location.isGranted) { startLocation(); } } }, heightRatioWithWidth: 0.82, ); }, ); } Position? latLng; saveLatLng(Position? latLng) async { SharedInstance.instance.saveLatLng(latLng); } @override Widget build(BuildContext context) { return Scaffold( body: Column( children: [ searchWidget(), Expanded( child: Container( child: ListView.separated( padding: EdgeInsets.symmetric(vertical: 0), itemCount: poiList?.length ?? 0, itemBuilder: (context, position) { return InkWell( onTap: () { Navigator.of(context).pop(poiList![position].toJson()); }, child: addressItem(poiList![position]), ); }, separatorBuilder: (context, position) { return Container( height: 1, margin: EdgeInsets.symmetric(horizontal: 16.w), color: Color(0xFFF1F1F1), ); }, ), ), flex: 1, ) ], ), ); } Widget addressItem(Address address) { return Container( margin: EdgeInsets.symmetric(vertical: 16.h, horizontal: 16.w), child: Column( mainAxisAlignment: MainAxisAlignment.spaceEvenly, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( address.cityInfo ?? "", overflow: TextOverflow.ellipsis, style: TextStyle( fontSize: 16.sp, fontWeight: FontWeight.bold, color: Colors.black, ), ), SizedBox( height: 4.h, ), Text( "${address.province}${address.city}${address.area}${address.addressStr}", overflow: TextOverflow.ellipsis, style: TextStyle( color: Color(0xFF4C4C4C), fontSize: 14.sp, ), ), ], ), ); } TextEditingController textEditingController = TextEditingController(); Widget searchWidget() { return Container( height: 36.h, margin: EdgeInsets.fromLTRB(16, 48, 16, 8), padding: EdgeInsets.fromLTRB(10, 6, 16, 6), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.all( Radius.circular(4), ), boxShadow: [ BoxShadow( color: Colors.black.withAlpha(12), offset: Offset(0, 3), blurRadius: 14, spreadRadius: 0, ), ], ), child: Row( mainAxisSize: MainAxisSize.max, children: [ Icon( Icons.search, size: 24, color: Colors.black, ), Expanded( child: TextField( controller: textEditingController, textInputAction: TextInputAction.search, onEditingComplete: () { FocusScope.of(context).requestFocus(FocusNode()); searchPoi(center); }, decoration: InputDecoration(border: InputBorder.none), ), ), InkWell( onTap: () { textEditingController.clear(); }, child: Icon( Icons.close, size: 19, color: Colors.grey, ), ), ], ), ); } }