0

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]

Srujan Simha
  • 3,637
  • 8
  • 42
  • 59
  • 2
    Have you tried setting the `coordinate` property? What happened? – Mundi Dec 12 '14 at 22:58
  • @Mundi Yes but it's not moving. I updated my post, check it. – Srujan Simha Dec 15 '14 at 16:24
  • Most of the time when you want to move something on the screen the movement are trigged by some event i.e. a timer. If you just set the coordinate in the for-loop it will set the coordinate to all location before the code completes a single run-loop. The screen will not be updated between the coordinate changes. – barksten Dec 15 '14 at 17:24
  • @barksten Yes but timer just gives the animation effect like a moving annotation but at the end there will be annotations at all the coordinate points on the route. I'm trying to do something like "Create an annotation at the first coordinate, run a loop through an array of coordinates, create the annotation at the next point while deleting it from the previous point (may be with a timer that gives animation effect)". I hope you understand. – Srujan Simha Dec 15 '14 at 17:37

2 Answers2

2

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
}
Mopsaopsa
  • 68
  • 1
  • 7
-1
[UIView animateWithDuration:5.0
                                  delay:0.0
                                options:UIViewAnimationOptionCurveLinear
                             animations:^{
                                 [CarAnnotationView.annotation setCoordinate:newCoord];
                             } completion:nil];
Wu Yang
  • 1
  • 2
  • 2
    Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this". We make an effort here to be a resource for knowledge. – Brian Tompsett - 汤莱恩 Sep 02 '16 at 15:40