Refresh annotations

1

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)
    }
    
asked by anonymous 16.06.2015 / 13:38

1 answer

1

It is being updated only when it opens because you are getting the location by the startUpdatingLocation() method and consequently its displayLocationInfo() method in viewDidLoad() .

To call every time you open this screen, you can get the location in viewDidAppear() :

override func viewDidAppear() {
    super.viewDidAppear()
    locManager.startUpdatingLocation()
}

But from what I understand of your code, you can optimize and get the location only once because there seems to be no need for the location all the time and then insert the annotations at each opening of the screen, so a second method would only serve for search this bank data:

func displayAnnotations() {
    mapRadar.removeAnnotations(mapRadar.annotations)

    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)!

    if results.count > 0 {
        for result in results {
            var anotacao = MKPointAnnotation()

            let r = result as! Radar

            anotacao.coordinate = CLLocationCoordinate2DMake((r.latitude as NSString).doubleValue, (r.longitude as NSString).doubleValue)
            anotacao.title = r.descricao
            anotacao.subtitle = "-_-"

            anot += [anotacao]
        }
    }

    mapRadar.addAnnotations(anot)
}

Note that I need to remove annotations to add new ones, otherwise it will be one on top of the other. And this method you call in viewDidAppear() .

Still a third option, if this second screen is opened from this map screen, you can create a delegate so that this above method can only be executed when a new one is needed insertion in the bank.

    
16.06.2015 / 14:24