Keep the (MKAnnotation) pin in the center when the user scrolls the map

I would like to keep MKAnnotaion in the center of the screen when the user feeds the card like the Careem app:

enter image description here

So far I've managed to show the pin, update the position of the pin, but when I scroll the map, the annotation in my code first moves and then returns to the center. I would like the pin to remain centered.

  @IBOutlet var mapView: MKMapView!
  var centerAnnotation = MKPointAnnotation()
  var manager:CLLocationManager!

 override func viewDidLoad() {
    super.viewDidLoad()
    manager = CLLocationManager() //instantiate
    manager.delegate = self // set the delegate
    manager.desiredAccuracy = kCLLocationAccuracyBest // required accurancy

    manager.requestWhenInUseAuthorization() // request authorization
    manager.startUpdatingLocation() //update location

    var lat = manager.location.coordinate.latitude // get lat
    var long = manager.location.coordinate.longitude // get long
    var coordinate = CLLocationCoordinate2DMake(lat, long)// set coordinate

    var latDelta:CLLocationDegrees = 0.01 // set delta
    var longDelta:CLLocationDegrees = 0.01 // set long
    var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
    var region:MKCoordinateRegion = MKCoordinateRegionMake(coordinate, span)

    self.mapView.setRegion(region, animated: true)
    centerAnnotation.coordinate = mapView.centerCoordinate
    self.mapView.addAnnotation(centerAnnotation)
}

func mapView(mapView: MKMapView!, regionDidChangeAnimated animated: Bool) {
    centerAnnotation.coordinate = mapView.centerCoordinate;
}

      

**** UPDATE ****

As suggested by Anna, I added a map view. Here's the code:

var newPoint = self.mapView.convertCoordinate(mapView.centerCoordinate, toPointToView: self.view)



    var pinImage = UIImage(named: "yoga_pins.png")
    var imageView = UIImageView(image: pinImage) // set as you want

    imageView.image = pinImage
    imageView.backgroundColor = UIColor.clearColor()
    imageView.contentMode = UIViewContentMode.Center
    imageView.center.y = newPoint.y
    imageView.center.x = newPoint.x


    self.view.addSubview(imageView)

      

the only problem is that when the map is loaded for the first time, the annotation that is on mapView.centerCoordinate

and I'm going to use it to get the latitude and longitude:

enter image description here

when i scroll the map the pin moves to the correct position (below the image):

    func mapView(mapView: MKMapView!, regionDidChangeAnimated animated: Bool) {
    centerAnnotation.coordinate = mapView.centerCoordinate;

}

      

enter image description here

+3


source to share


1 answer


You can use a custom button. Add this button to the center of the map. Just show and hide this button according to your conditions. Therefore, when you move the map, this button remains in the same place as in the center of the map.see center button selector is not available.



0


source







All Articles