You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
394 lines
13 KiB
394 lines
13 KiB
import 'dart:convert'; |
|
|
|
import 'package:amap_flutter_location/amap_flutter_location.dart'; |
|
import 'package:amap_flutter_base/amap_flutter_base.dart'; |
|
import 'package:amap_flutter_location/amap_location_option.dart'; |
|
import 'package:amap_flutter_map/amap_flutter_map.dart'; |
|
import 'package:dio/dio.dart'; |
|
import 'package:flutter/foundation.dart'; |
|
import 'package:flutter/gestures.dart'; |
|
import 'package:flutter/material.dart'; |
|
import 'package:flutter/services.dart'; |
|
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; |
|
import 'package:huixiang/retrofit/data/address.dart'; |
|
import 'package:huixiang/retrofit/retrofit_api.dart'; |
|
import 'package:permission_handler/permission_handler.dart'; |
|
import 'package:shared_preferences/shared_preferences.dart'; |
|
import 'package:flutter_screenutil/flutter_screenutil.dart'; |
|
|
|
class AddressMapPage extends StatefulWidget { |
|
@override |
|
State<StatefulWidget> createState() { |
|
return _AddressMapPage(); |
|
} |
|
} |
|
|
|
class _AddressMapPage extends State<AddressMapPage> { |
|
//默认设置为不使用自定义地图,如果需要直接显示,在初始化是设置为true |
|
CustomStyleOptions _customStyleOptions = CustomStyleOptions(false); |
|
|
|
//加载自定义地图样式 |
|
void _loadCustomData() async { |
|
if (null == _customStyleOptions) { |
|
_customStyleOptions = CustomStyleOptions(false); |
|
} |
|
ByteData styleByteData = |
|
await rootBundle.load('assets/map_style/style.data'); |
|
_customStyleOptions.styleData = styleByteData.buffer.asUint8List(); |
|
ByteData styleExtraByteData = |
|
await rootBundle.load('assets/map_style/style_extra.data'); |
|
_customStyleOptions.styleExtraData = |
|
styleExtraByteData.buffer.asUint8List(); |
|
//如果需要加载完成后直接展示自定义地图,可以通过setState修改CustomStyleOptions的enable为true |
|
setState(() { |
|
_customStyleOptions.enabled = true; |
|
}); |
|
} |
|
|
|
AMapFlutterLocation aMapFlutterLocation; |
|
String city = "武汉市"; |
|
String keyWord = ""; |
|
|
|
@override |
|
void dispose() { |
|
super.dispose(); |
|
aMapFlutterLocation.stopLocation(); |
|
aMapFlutterLocation.destroy(); |
|
} |
|
|
|
ApiService apiService; |
|
|
|
@override |
|
void initState() { |
|
super.initState(); |
|
|
|
SharedPreferences.getInstance().then((value) => { |
|
apiService = ApiService(Dio(), |
|
context: context, token: value.getString('token')), |
|
}); |
|
|
|
if (aMapFlutterLocation == null) { |
|
AMapFlutterLocation.setApiKey("f39d1daa020a56f208eb2519f63e9534", |
|
"feaae7986201b571cace1b83728be5bb"); |
|
aMapFlutterLocation = AMapFlutterLocation(); |
|
aMapFlutterLocation.onLocationChanged().listen((event) { |
|
print("event: ${jsonEncode(event)}"); |
|
if (event != null && |
|
event["latitude"] != null && |
|
event["longitude"] != null) { |
|
city = event["city"]; |
|
LatLng latLng; |
|
if (event["latitude"] is String && event["longitude"] is String) { |
|
latLng = LatLng(double.tryParse(event["latitude"]), |
|
double.tryParse(event["longitude"])); |
|
} else { |
|
latLng = LatLng(event["latitude"], event["longitude"]); |
|
} |
|
saveLatLng(latLng); |
|
if (_mapController != null) |
|
_mapController.moveCamera( |
|
CameraUpdate.newCameraPosition( |
|
CameraPosition( |
|
target: latLng, |
|
zoom: 15.0, |
|
), |
|
), |
|
); |
|
searchPoi(latLng); |
|
} |
|
}); |
|
} |
|
aMapFlutterLocation.setLocationOption( |
|
AMapLocationOption( |
|
needAddress: true, |
|
onceLocation: true, |
|
locationMode: AMapLocationMode.Hight_Accuracy, |
|
pausesLocationUpdatesAutomatically: true, |
|
), |
|
); |
|
|
|
_loadCustomData(); |
|
|
|
getLatLng(); |
|
startLocation(); |
|
} |
|
|
|
List<Address> poiList; |
|
|
|
searchPoi(LatLng latLng) async { |
|
keyWord = textEditingController.text; |
|
var addressPoi = await apiService.searchPoi( |
|
"${latLng.latitude}", "${latLng.longitude}", keyWord, 20, 1); |
|
List<dynamic> poi = addressPoi['pois']; |
|
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(() {}); |
|
} |
|
|
|
//{"parent":[],"distance":"7","pcode":"420000","importance":[], |
|
// "recommend":"0","type":"商务住宅;住宅区;住宅区","photos":[],"discount_num":"0","gridcode":"4514626711", |
|
// "typecode":"120300","shopinfo":"0","poiweight":[],"citycode":"027","adname":"武昌区","children":[], |
|
// "alias":[],"tel":[],"id":"B0G2YMOFHL","tag":[],"event":[],"entr_location":[],"indoor_map":"0", |
|
// "email":[],"timestamp":"2021-06-02 00:19:39","website":[],"address":"中北路86号","adcode":"420106", |
|
// "pname":"湖北省","biz_type":[],"cityname":"武汉市","postcode":[],"match":"0","business_area":"中北路", |
|
// "indoor_data":,"childtype":[],"exit_location":[], |
|
// "name":"万象春天","location":"114.343968,30.554595","shopid":[],"navi_poiid":[],"groupbuy_num":"0"} |
|
startLocation() async { |
|
// await AmapCore.init('feaae7986201b571cace1b83728be5bb'); |
|
if (await Permission.locationWhenInUse.serviceStatus.isEnabled) { |
|
bool isShown = await Permission.contacts.shouldShowRequestRationale; |
|
// if (isShown) { |
|
// SmartDialog.showToast("shouldShowRequest"); |
|
// } |
|
if (await Permission.location.isPermanentlyDenied) { |
|
//openAppSettings |
|
} else if (await Permission.location.isGranted) { |
|
aMapFlutterLocation.startLocation(); |
|
} else { |
|
await Permission.location.request(); |
|
startLocation(); |
|
} |
|
} else { |
|
//enabledLocation |
|
// _mapController.getMapContentApprovalNumber() |
|
// _mapController |
|
} |
|
} |
|
|
|
AMapController _mapController; |
|
|
|
void onMapCreated(AMapController controller) { |
|
setState(() { |
|
_mapController = controller; |
|
}); |
|
} |
|
|
|
saveLatLng(LatLng latLng) async { |
|
SharedPreferences prefs = await SharedPreferences.getInstance(); |
|
await prefs.setString("latitude", "${latLng.latitude}"); |
|
await prefs.setString("longitude", "${latLng.longitude}"); |
|
} |
|
|
|
getLatLng() async { |
|
SharedPreferences.getInstance().then((value) => { |
|
setState(() { |
|
if (_mapController != null) { |
|
_mapController |
|
.moveCamera(CameraUpdate.newCameraPosition(CameraPosition( |
|
target: LatLng(double.tryParse(value.getString("latitude")), |
|
double.tryParse(value.getString("longitude"))), |
|
zoom: 15.0, |
|
))); |
|
} |
|
}) |
|
}); |
|
} |
|
|
|
AMapWidget map; |
|
LatLng center = LatLng(30.553111, 114.342366); |
|
|
|
@override |
|
Widget build(BuildContext context) { |
|
AMapApiKey aMapApiKeys = AMapApiKey( |
|
androidKey: 'f39d1daa020a56f208eb2519f63e9534', |
|
iosKey: 'feaae7986201b571cace1b83728be5bb'); |
|
return Scaffold( |
|
body: Column( |
|
children: [ |
|
Expanded( |
|
child: Stack( |
|
children: [ |
|
Container( |
|
child: AMapWidget( |
|
initialCameraPosition: CameraPosition( |
|
target: LatLng(30.553111, 114.342366), |
|
zoom: 12.0, |
|
), |
|
onMapCreated: onMapCreated, |
|
onCameraMoveEnd: (cameraPosition) { |
|
center = cameraPosition.target; |
|
searchPoi(center); |
|
}, |
|
onCameraMove: (cameraPosition) { |
|
center = cameraPosition.target; |
|
setState(() {}); |
|
}, |
|
apiKey: aMapApiKeys, |
|
// markers: [Marker(position: center)].toSet(), |
|
touchPoiEnabled: true, |
|
scrollGesturesEnabled: true, |
|
customStyleOptions: _customStyleOptions, |
|
gestureRecognizers: <Factory<OneSequenceGestureRecognizer>>[ |
|
Factory<OneSequenceGestureRecognizer>( |
|
() => EagerGestureRecognizer()) |
|
].toSet(), |
|
), |
|
), |
|
Center( |
|
child: IgnorePointer( |
|
child: Container( |
|
margin: EdgeInsets.only(bottom: 44.h), |
|
child: Image.asset( |
|
"assets/image/icon_address_location.png", |
|
width: 66.w, |
|
height: 75.h, |
|
), |
|
), |
|
), |
|
), |
|
searchWidget(), |
|
Positioned( |
|
right: 17, |
|
bottom: 16, |
|
child: InkWell( |
|
onTap: () { |
|
startLocation(); |
|
}, |
|
child: Container( |
|
width: 32.w, |
|
height: 32.h, |
|
padding: EdgeInsets.all(8), |
|
decoration: BoxDecoration( |
|
borderRadius: BorderRadius.circular(16), |
|
color: Colors.white, |
|
), |
|
child: Image.asset( |
|
"assets/image/icon_address_location_action.png", |
|
width: 16.w, |
|
height: 16.h, |
|
), |
|
), |
|
), |
|
) |
|
], |
|
), |
|
flex: 1, |
|
), |
|
Expanded( |
|
child: Container( |
|
child: ListView.separated( |
|
padding: EdgeInsets.symmetric(vertical: 0), |
|
itemCount: poiList != null ? poiList.length : 0, |
|
itemBuilder: (context, position) { |
|
return InkWell( |
|
onTap: () { |
|
Navigator.of(context).pop(poiList[position].toJson()); |
|
}, |
|
child: addressItem(poiList[position]), |
|
); |
|
}, |
|
separatorBuilder: (contetx, 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.address}", |
|
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, |
|
), |
|
), |
|
], |
|
), |
|
); |
|
} |
|
}
|
|
|