It’s kind of obvious in retrospect about enabling or disabling animation. It’s part of some code where I’ve added a UIImage onto a MkMapView (Apple’s iOS Controls for displaying a map), that centres the map on your current location when you click it. The button is a gun sight type icon and I’ve located in the bottom right edge of the map.

    private UIButton btnCurrentLoc; // defined at the class level

// in ViewDidLoad
    var ImageCurrentLoc = UIImage.FromBundle("images/currentloc.png");
    ImageCurrentLoc.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);

    btnCurrentLoc = new UIButton() { TintColor = UIColor.Black };
    btnCurrentLoc.SetImage(ImageCurrentLoc, UIControlState.Normal);
    btnCurrentLoc.Frame = new RectangleF(View.Frame.Width-50, View.Frame.Height - 100, ImageCurrentLoc.Size.Width, ImageCurrentLoc.Size.Height);

    btnCurrentLoc.TouchUpInside += (s, e) =>
                {
                    map.SetCenterCoordinate(map.UserLocation.Location.Coordinate,true);  // animated
                    //map.CenterCoordinate = map.UserLocation.Location.Coordinate;       // not animated, moves directly            
                }; 

  View.AddSubview(map);            // has to be this way round! map first then control
  View.AddSubview(btnCurrentLoc);

Note that you add the map first to the view then the button.

As it’s commented if you click the btnCurrentLoc button, it will animate the map so you can see it scroll to your location. If you comment out that line and uncomment the line below, when you click the button it will move there immediately.

iPhone MkMapView with home button

Leave a Reply

Post Navigation