Skip to content

Commit 1ecdb42

Browse files
committed
fix: Added MKUserTrackingButton
1 parent 01e3ea4 commit 1ecdb42

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

example/lib/map_ui.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class MapUiBodyState extends State<MapUiBody> {
4848
bool _pitchGesturesEnabled = true;
4949
bool _zoomGesturesEnabled = true;
5050
bool _myLocationEnabled = true;
51+
TrackingMode _trackingMode = TrackingMode.none;
5152
AppleMapController _controller;
5253
@override
5354
void initState() {
@@ -170,6 +171,7 @@ class MapUiBodyState extends State<MapUiBody> {
170171
Widget build(BuildContext context) {
171172
final AppleMap appleMap = AppleMap(
172173
onMapCreated: onMapCreated,
174+
trackingMode: _trackingMode,
173175
initialCameraPosition: _kInitialPosition,
174176
compassEnabled: _compassEnabled,
175177
minMaxZoomPreference: _minMaxZoomPreference,
@@ -180,6 +182,7 @@ class MapUiBodyState extends State<MapUiBody> {
180182
zoomGesturesEnabled: _zoomGesturesEnabled,
181183
myLocationEnabled: _myLocationEnabled,
182184
myLocationButtonEnabled: _myLocationButtonEnabled,
185+
padding: const EdgeInsets.all(10),
183186
onCameraMove: _updateCameraPosition,
184187
);
185188

ios/Classes/MapView/FlutterMapView.swift

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ class FlutterMapView: MKMapView, UIGestureRecognizerDelegate {
114114
if let isCompassEnabled: Bool = options["compassEnabled"] as? Bool {
115115
if #available(iOS 9.0, *) {
116116
self.showsCompass = isCompassEnabled
117+
self.mapTrackingButton(isVisible: self.isMyLocationButtonShowing ?? false)
117118
}
118119
}
119120

@@ -214,20 +215,38 @@ class FlutterMapView: MKMapView, UIGestureRecognizerDelegate {
214215
// Functions used for the mapTrackingButton
215216
func mapTrackingButton(isVisible visible: Bool) {
216217
self.isMyLocationButtonShowing = visible
218+
if let _locationButton = self.viewWithTag(BUTTON_IDS.LOCATION.rawValue) {
219+
_locationButton.removeFromSuperview()
220+
}
217221
if visible {
218-
let image = UIImage(named: "outline_near_me")
219-
let locationButton = UIButton(type: UIButton.ButtonType.custom) as UIButton
220-
locationButton.tag = BUTTON_IDS.LOCATION.rawValue
221-
locationButton.layer.cornerRadius = 5
222-
locationButton.frame = CGRect(origin: CGPoint(x: self.bounds.width - 45, y: self.bounds.height - 45), size: CGSize(width: 40, height: 40))
223-
locationButton.setImage(image, for: .normal)
224-
locationButton.backgroundColor = .white
225-
locationButton.alpha = 0.8
226-
locationButton.addTarget(self, action: #selector(centerMapOnUserButtonClicked), for:.touchUpInside)
227-
self.addSubview(locationButton)
228-
} else {
229-
if let _locationButton = self.viewWithTag(BUTTON_IDS.LOCATION.rawValue) {
230-
_locationButton.removeFromSuperview()
222+
let buttonContainer = UIView()
223+
if #available(iOS 9.0, *) {
224+
buttonContainer.translatesAutoresizingMaskIntoConstraints = false
225+
buttonContainer.widthAnchor.constraint(equalToConstant: 35).isActive = true
226+
buttonContainer.heightAnchor.constraint(equalToConstant: 35).isActive = true
227+
buttonContainer.layer.cornerRadius = 8
228+
buttonContainer.tag = BUTTON_IDS.LOCATION.rawValue
229+
buttonContainer.backgroundColor = .white
230+
if #available(iOS 11.0, *) {
231+
let userTrackingButton = MKUserTrackingButton(mapView: self)
232+
userTrackingButton.translatesAutoresizingMaskIntoConstraints = false
233+
buttonContainer.addSubview(userTrackingButton)
234+
userTrackingButton.centerXAnchor.constraint(equalTo: buttonContainer.centerXAnchor).isActive = true
235+
userTrackingButton.centerYAnchor.constraint(equalTo: buttonContainer.centerYAnchor).isActive = true
236+
} else {
237+
let locationButton = UIButton(type: UIButton.ButtonType.custom) as UIButton
238+
let image = UIImage(named: "outline_near_me")
239+
locationButton.translatesAutoresizingMaskIntoConstraints = false
240+
locationButton.setImage(image, for: .normal)
241+
locationButton.imageView?.tintColor = .blue
242+
locationButton.addTarget(self, action: #selector(centerMapOnUserButtonClicked), for:.touchUpInside)
243+
buttonContainer.addSubview(locationButton)
244+
locationButton.centerXAnchor.constraint(equalTo: buttonContainer.centerXAnchor).isActive = true
245+
locationButton.centerYAnchor.constraint(equalTo: buttonContainer.centerYAnchor).isActive = true
246+
}
247+
self.addSubview(buttonContainer)
248+
buttonContainer.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -5 - self.layoutMargins.right).isActive = true
249+
buttonContainer.topAnchor.constraint(equalTo: self.topAnchor, constant: self.showsCompass ? 50 : 5 + self.layoutMargins.top).isActive = true
231250
}
232251
}
233252
}

0 commit comments

Comments
 (0)