Unlimited number of pins on the map, and saving them in CoreData.

Hello. I'm trying to working in Xcode only first month.


How can I realize the possibility of setting unlimited number of pins on the map? One tap on map - pin. One more tap in others coordinates - one more pin on this coordinates. How do it unlimited? And how can i save all this coordinates (on pins) to CoreData? And on the next start programm, all the data (coordinates of each pin) is read from the CoreData and show pins on the map?

Accepted Answer

up

Such "how do I write an app" questions rarely get useful answers. I suggest breaking that rather large problem down into smaller chunks, such as

- tap on map to add a pin

- store that one pin's location in persistent storage (I recommend NSUserDefaults to start - it's MUCH simpler than Core Data)

- load the pin from persistent storage on app start and display it on the map


If you have specific questions, post detailed error message / description, code that reproduces the problem, and details of what troubleshooting steps you've taken already. Once you have the "proof of concept" of storing one pin working, then hit the books (figuratively) and start learning about Core Data. It has a steep learning curve. You can substitute the NSUserDefaults piece with Core Data once you have that figured out.


Adding unlimited numbers of pins is the default. MapKit doesn't enforce any limits. You may run into performance problems once you get past a few hundred though, so you'll likely have to implement clustering at some point. Apple has sample code for that. But again, breaking it down into smaller pieces, ignore that for now until you have the basics working.

Revised per Comments


You need to translate this logic into code and it is fairly simple and I am also providing a sample.


  1. The MKMapView collects all annotations into a sinlge property called annotations (these will hold all annotations on the mapview including MKPinAnnotation)
  2. The behavior of unlimited annotations is default behavior
  3. You could add a tap gesture to make as many pins as you like and use the hitTest to convert the touch point to a coordinate (then make the MKPinAnnotation at that coordinate)
  4. To save and retrieve from Core Data you can just wrap the entire map view or all of its annotations as a single property into an object graph (see my sample where I save a CLLocation and substitute either the entire map view or the array of annotations instead)
  5. You can start a master - detail template to get the default Core Data logic and use the sample above to capture the map view or just it's MKPinAnnotations.


One key part to this solution is the need to convert the tap gesture into a coordinate point


- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{ 
    CLLocationCoordinate2D coord= [map convertPoint:point toCoordinateFromView:map]; 
    NSLog(@"lat %f",coord.latitude); 
    NSLog(@"long %f",coord.longitude); 
    ... add annotation ... 
    return [super hitTest:point withEvent:event]; 
}


Here's some sample on making an MKPointAnnotation also stored in the annotations array. Adding MKPinAnnotation is just as simple.


- (void)viewDidLoad { 
     UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(foundTap:)]; 
     tapRecognizer.numberOfTapsRequired = 1; 
     tapRecognizer.numberOfTouchesRequired = 1; 
     [self.myMapView addGestureRecognizer:tapRecognizer]; 
}

-(IBAction)foundTap:(UITapGestureRecognizer *)recognizer { 
    CGPoint point = [recognizer locationInView:self.myMapView]; 
    CLLocationCoordinate2D tapPoint = [self.myMapView convertPoint:point toCoordinateFromView:self.view];
    MKPointAnnotation *point1 = [[MKPointAnnotation alloc] init]; 
    point1.coordinate = tapPoint; 
    [self.myMapView addAnnotation:point1]; 
}

Please, give me a small example (code) how add pin on map by tap? I do not know the methods that are responsible for it. And how i can check this in xcode? I can't tapping, because i working with mouse and keyboard.


sorry for my bad english, and thanks.

i dont need annotations, i need unlimited count of pins (flags) on the map, and the ability to save this in the coredata.

Please see my revisions and don't forget to let me know if more clarity is needed. Otherwise feel free to accept the answer as revised.

This can't be the answer. It says nothing.

Unlimited number of pins on the map, and saving them in CoreData.
 
 
Q