swiftannotationsmkpointannotation

swift: Move annotation from one coordinate to another


I have been trying to move annotation (custom) on map from one coordinate to another but couldn't find a better way to do it. I have searched online but couldn't find anything useful.

Anyone who was successful in implementing such functionality (in swift)?

EDIT

var annMove: CustomAnnotation = CustomAnnotation();
    annMove.title = "Moving annotation"
    annMove.subtitle = ""
    annMove.imageName = "blue_pin.png"
    self.mapView.addAnnotation(annMove);
    for i in 0..<route.count
    {
        annMove.coordinate = route[i];
    }

CustomAnnotation

class CustomAnnotation: MKPointAnnotation {
var imageName: String!

}

When I ran the code, all I can see is an annotation at coordinate route[0]. If the annotation did move then at least the it should be at the coordinate route[route.count-1] but not route[0]


Solution

  • I hope this might help. It worked for me.

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    
        var userLocation: CLLocation = locations[0] as! CLLocation
    
        var latitude = userLocation.coordinate.latitude
        var longitude = userLocation.coordinate.longitude
    
        var latDelta:CLLocationDegrees = 0.05
        var lonDelta: CLLocationDegrees = 0.05
    
        var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
        var location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
        var region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
    
        self.map.setRegion (region, animated: false)
    }
    
    override func viewDidLoad() {
       super.viewDidLoad()
    
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
        map.showsUserLocation = true
    }