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.

290 lines
8.5 KiB

import 'dart:convert';
import 'package:amap_flutter_location/amap_flutter_location.dart';
import 'package:amap_flutter_base/amap_flutter_base.dart' as latlng;
import 'package:amap_flutter_location/amap_location_option.dart';
import 'package:amap_flutter_map/amap_flutter_map.dart';
// import 'package:amap_search_fluttify/amap_search_fluttify.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:shared_preferences/shared_preferences.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();
// AmapSearch.instance.dispose();
}
@override
void initState() {
super.initState();
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 latLng;
if (event["latitude"] is String && event["longitude"] is String) {
latLng = latlng.LatLng(double.tryParse(event["latitude"]),
double.tryParse(event["longitude"]));
} else {
latLng = 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();
}
searchPoi(latlng.LatLng latLng) async {
// var poiList = await AmapSearch.instance.searchAround(
// LatLng(latLng.latitude, latLng.longitude),
// keyword: keyWord,
// city: city,
// );
// markers = poiList.map(
// (e) => Marker(
// position: latlng.LatLng(e.latLng.latitude, e.latLng.longitude),
// anchor: Offset(0.5, 0.8),
// icon: BitmapDescriptor.fromIconPath(
// "assets/image/icon_address_location.png"),
// ),
// );
setState(() {});
}
List<Marker> markers;
startLocation() async {
// await AmapCore.init('feaae7986201b571cace1b83728be5bb');
if (await Permission.locationWhenInUse.serviceStatus.isEnabled) {
bool isShown = await Permission.contacts.shouldShowRequestRationale;
if (isShown) {
Fluttertoast.showToast(
msg: "shouldShowRequest",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0);
}
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 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.LatLng(
double.tryParse(value.getString("latitude")),
double.tryParse(value.getString("longitude"))),
zoom: 15.0,
)));
}
})
});
}
AMapWidget map;
@override
Widget build(BuildContext context) {
latlng.AMapApiKey aMapApiKeys = latlng.AMapApiKey(
androidKey: 'f39d1daa020a56f208eb2519f63e9534',
iosKey: 'feaae7986201b571cace1b83728be5bb');
if (map == null) {
map = AMapWidget(
initialCameraPosition: CameraPosition(
target: latlng.LatLng(30.553111, 114.342366),
zoom: 12.0,
),
onMapCreated: onMapCreated,
apiKey: aMapApiKeys,
touchPoiEnabled: true,
markers: markers.toSet(),
scrollGesturesEnabled: true,
customStyleOptions: _customStyleOptions,
gestureRecognizers: <Factory<OneSequenceGestureRecognizer>>[
Factory<OneSequenceGestureRecognizer>(() => EagerGestureRecognizer())
].toSet(),
);
}
return Scaffold(
body: Column(
children: [
Expanded(
child: Stack(
children: [
Container(
child: map,
),
searchWidget(),
],
),
flex: 1,
),
Expanded(
child: Container(),
flex: 1,
)
],
),
);
}
Widget searchWidget() {
return Container(
height: 36,
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(
children: [
InkWell(
onTap: () {},
child: Container(
alignment: Alignment.center,
padding: EdgeInsets.fromLTRB(10, 0, 6, 0),
child: Text(
"武汉市",
style: TextStyle(
fontSize: 12,
color: Colors.black,
),
),
),
),
Container(
width: 1,
color: Colors.black,
margin: EdgeInsets.only(top: 2, bottom: 2, left: 10, right: 16),
),
Icon(
Icons.search,
size: 24,
color: Colors.black,
),
Expanded(
child: TextField(
decoration: InputDecoration(border: InputBorder.none),
),
),
Icon(
Icons.close,
size: 19,
color: Colors.grey,
),
],
),
);
}
}