I need to update annotations on the map after inserting values in CoreData. I have the first view with the map, and a second where I add the new values to new annotations, but it is only when I enter the application for the first time that the annotations appear updated. I think I need to load the map every time I enter the first view controller. How can I update the values (annotations)? this is the note and map code:
override func viewDidLoad() {
super.viewDidLoad()
self.locManager.delegate = self
self.locManager.desiredAccuracy = kCLLocationAccuracyBest
self.locManager.requestWhenInUseAuthorization()
self.locManager.startUpdatingLocation()
latitude = lat.text!
longitude = long.text!
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: {(placemarks, error)->Void in
if (error != nil)
{
println("Error: " + error.localizedDescription)
return
}
if placemarks.count > 0
{
let pm = placemarks[0] as! CLPlacemark
self.displayLocationInfo(pm)
}
else
{
println("Error with the data.")
}
})
}
func displayLocationInfo(placemark:CLPlacemark){
println("display")
self.locManager.stopUpdatingLocation()
lat.text = String(stringInterpolationSegment: placemark.location.coordinate.latitude);
long.text = String(stringInterpolationSegment: placemark.location.coordinate.longitude);
latitude = lat.text!
longitude = long.text!
var latD:CLLocationDegrees = 0.1
var longD:CLLocationDegrees = 0.1
var la:CLLocationDegrees = (latitude as NSString).doubleValue
var lo:CLLocationDegrees = (longitude as NSString).doubleValue
var span:MKCoordinateSpan = MKCoordinateSpanMake(latD, longD)
var locat:CLLocationCoordinate2D = CLLocationCoordinate2DMake(la, lo)
var locat2:CLLocationCoordinate2D = CLLocationCoordinate2DMake(la+1, lo+1)
var reg:MKCoordinateRegion = MKCoordinateRegionMake(locat, span)
mapRadar.setRegion(reg, animated: true)
//Anotações
var anot = [MKPointAnnotation]()
var request:NSFetchRequest = NSFetchRequest(entityName: "Radar")
let appDelegate:AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
let context:NSManagedObjectContext = appDelegate.managedObjectContext!
request.returnsObjectsAsFaults = false
var results:NSArray = context.executeFetchRequest(request, error: nil)!
//println(results.count)
//println(results.objectAtIndex(0))
if results.count > 0
{
for result in results
{
var anotacao = MKPointAnnotation()
let r = result as! Radar
// println(r.velocidade)
locat.latitude = (r.latitude as NSString).doubleValue
locat.longitude = (r.longitude as NSString).doubleValue
anotacao.coordinate = locat
anotacao.title = r.descricao
anotacao.subtitle = "-_-" //String(_cocoaString: r.velocidade)
anot += [anotacao]
}
}
self.mapRadar.addAnnotations(anot)
}