@@ -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