0
votes

In my app a user searches for a location on the map and a pin (MKPinAnnotationView) is dropped on that specific location. I'm using Core Data to store info about that location (such as city name, latitude, longitude, etc.)

I want to persist the MKAnnotationViews so that when the user reopens the app the "pins" are still there.

What's the proper way to handle this? I'm already storing the latitude and longitude in CoreData, but I would really like to store the entire MKAnnotation with its title, subtitle and other attributes directly in CoreData and then just redraw them on the map when the app is opened.

I'm using Objective-C not Swift.

1

1 Answers

0
votes

Not sure what your issue is. I'm doing this in Swift not ObjC, but in essence you just:

  • Get the data for the points from CoreData,
  • Load it into an array of annotation instances (I've actually got a more complex class, Waypoint, that's sub-classed from NSObject (but follows the Annotation protocol) since I need more info for custom pins and polyline drawing, etc., but that shouldn't change the MapView process), and
  • Add / show the Annotations in Mapview as I assume that you did the first time.

Given the added data, my setup of the Waypoint objects is a more complex init method so I'm not showing that, but your setup should be the same as the first time you loaded the annotation values. Here's the Swift 3.0 version of the load to MapView which is straightforward in Swift - but it shouldn't be much different in ObjC since it's just calling the APIs:

 // Display a set of Waypoints for a Track

 private func handleWaypoints(_ waypoints: [MKAnnotation])
 {
     mapView.addAnnotations(waypoints)
     mapView.showAnnotations(waypoints, animated: false)
 }

Since I cycle through maps, I also use calls to be sure the map is clean before I do the loads above, which is just:

private func clearAnnotations()
{
    if mapView?.annotations != nil { mapView.removeAnnotations(mapView.annotations ) }
    if mapView?.overlays != nil {mapView.removeOverlays(mapView.overlays)}
}

Does this help or am I missing your concern?