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.
 
 
 
 
 
 

144 lines
4.1 KiB

import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter_baidu_mapapi_base/flutter_baidu_mapapi_base.dart';
import 'package:flutter_baidu_mapapi_map/flutter_baidu_mapapi_map.dart';
import 'package:flutter_bmflocation/flutter_bmflocation.dart';
import 'package:huixiang/utils/flutter_utils.dart';
import 'package:huixiang/utils/location.dart';
import 'package:huixiang/view_widget/my_appbar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
class LocationMap extends StatefulWidget {
final Map<String, String> arguments;
LocationMap({this.arguments});
@override
State<StatefulWidget> createState() {
return _LocationMap();
}
}
class _LocationMap extends State<LocationMap> {
@override
void initState() {
super.initState();
startLocation();
}
void startLocation() {
Location.getInstance().startLocation(context, (BaiduLocation result) {
if (result != null &&
result.latitude != null &&
result.longitude != null) {
print("location: $result");
myLatLng = BMFCoordinate(result.latitude, result.longitude);
AppUtils.coordConvert(myLatLng).then((value) {
this.myLatLng = value;
locationShow();
});
}
});
}
locationShow() {
BMFLocation location = BMFLocation(
coordinate: myLatLng,
altitude: 0,
horizontalAccuracy: 5,
verticalAccuracy: -1.0,
speed: -1.0,
course: -1.0,
);
BMFUserLocation userLocation = BMFUserLocation(
location: location,
);
setState(() {
_mapController.updateLocationData(userLocation);
});
}
@override
void dispose() {
super.dispose();
Location.getInstance().stopLocation();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: MyAppBar(
background: Color(0xFFF7F7F7),
title: widget.arguments["storeName"],
titleColor: Colors.black87,
titleSize: 18.sp,
leadingColor: Colors.black,
),
body: Container(
//BMFMapWidget 组件会自动默认沾满全屏,并且挡住所有遮盖物 ,BMFTextureMapWidget不会强制遮盖其他控件。
child:BMFMapWidget(
mapOptions: BMFMapOptions(
center: BMFCoordinate(
double.tryParse(widget.arguments["lat"]),
double.tryParse(widget.arguments["lng"]),
),
showZoomControl: false,
showMapScaleBar: false,
zoomLevel: 12,
),
onBMFMapCreated: onMapCreated,
)
),
);
}
BMFMapController _mapController;
BMFCoordinate latLng;
BMFCoordinate myLatLng;
BMFMarker bmfMarker;
onMapCreated(BMFMapController controller) {
_mapController = controller;
setState(() {
_mapController.showUserLocation(true);
_mapController.setCustomMapStyle('assets/map_style/chatian.sty', 0);
BMFUserLocationDisplayParam displayParam = BMFUserLocationDisplayParam(
locationViewOffsetX: 0,
locationViewOffsetY: 0,
accuracyCircleFillColor: Colors.red,
accuracyCircleStrokeColor: Colors.blue,
isAccuracyCircleShow: true,
locationViewImage: 'assets/image/icon_my_location.webp',
locationViewHierarchy:
BMFLocationViewHierarchy.LOCATION_VIEW_HIERARCHY_BOTTOM,
);
_mapController.updateLocationViewWithParam(displayParam);
addMarker();
});
}
addMarker() async {
// latLng = await AppUtils.coordConvert(BMFCoordinate(double.tryParse(widget.arguments["lat"]),
// double.tryParse(widget.arguments["lng"])));
latLng = BMFCoordinate(double.tryParse(widget.arguments["lat"]),
double.tryParse(widget.arguments["lng"]));
if (bmfMarker == null && _mapController != null) {
bmfMarker = BMFMarker(
position: latLng,
centerOffset: BMFPoint(0.5, 0.7),
enabled: false,
icon: "assets/image/icon_map_marker.webp",
draggable: false,
);
_mapController.addMarker(bmfMarker);
}
_mapController.updateMapOptions(
BMFMapOptions(
center: latLng,
zoomLevel: 15,
),
);
}
}