Remove MKAnnotation from MapView - iphone

Ok, I can add pin on my map by LongPress on any place of map. Now I need to delete pins. So I want next: when I click on my pin, appear the name of pin and a little button with cross ((X) like in all apps to close), when user will click this button (X) - pin must be deleted. Can I do this? Or maybe there is another simple way to delete pin for user without go to detailview about this pin?

For remove all annotation use this code.
[yourMapView removeAnnotation:yourMapView.annotations];
For remove one annotation just implement logic For example...
First remove all annotation and also remove your selected pin data from the array and after add this new array and add annotation..

// REMOVING ALL ANNOTATION
for (id <MKAnnotation> myAnnot in [objMapView annotations])
{
if (![myAnnot isKindOfClass:[MKUserLocation class]])
{
[objMapView removeAnnotation:myAnnot];
}
}

Related

MapView annotation View without Clicking

In my application show Multiple Annotation between two city but when we move one Annotation to other (with map region set on next previous Button) how to show Annotation detail automatic without clicking on it???
you can test with the title of the annotation, if it mach, you can use
selectAnnotation: animated:
to show the Annotation ( you can subclass MKAnnotation and add an id for test instead title ):
for (id<MKAnnotation> annotation in mapView.annotations)
{
if( [annotation.title isEqualToString:title]) [mapView selectAnnotation:annotation animated:YES];
}

Duplicated MKAnnotations when charging web service in xCode

I make annotations from a response JSON from a Web service.
I can load the pins in the map, but when I move the position of the center I have to reload a new bunch of annotations and delete the old ones. When I do the method it only charges the pins and if I move the center it does the same and recharge.
I've tried a lot of methods like this one...
for (id<MKAnnotation> annotation in _mapView.annotations) {
[_mapView removeAnnotation:annotation];
}
But it only, does this: I gets to the center of the map and when I move the map it returns to the start and I can't zoom the map cause it reloads and loops.
To remove all annotations, just run this before you add the new ones..
NSMutableArray *toRemove = [NSMutableArray arrayWithCapacity:15];
for (id annotation in mapView.annotations){
if (annotation != mapView.userLocation)
[toRemove addObject:annotation];
[mapView removeAnnotations:toRemove];

Customize the MKAnnotationView callout

I want to create a custom MKAnnotationView callout as shown in this image. I have tested several solutions but they only allow customization of the left/right images and title/subtitle. Can anybody please give me some source code or tutorial link for it?
Currently I am clueless. Please help.
I understand you want a pin with a custom callout.
We can't create a custom callout, but we can create an annotation with a completely customized view. So the trick is to add a second annotation when the first is selected, and make the 2nd annotation view look like a callout bubble.
This is the solution posted by users djibouti33 and jacob-jennings in the answer: MKAnnotationView - Lock custom annotation view to pin on location updates, which in turn is based in a blog post from Asynchrony Solutions. For explanation purposes, here is some UML from a forked project:
This is a big hack, but also the cleanest way I've seen to implement custom annotations.
Start with a NSObject "Content" class which has a coordinate, the class of the callout view to use (in the UML is AnnotationView, but you can create more and set them here), and a dictionary of random values with the title, photo url, etc. Use this class to initialize a MKAnnotation "Annotation" object.
#import <MapKit/MapKit.h>
#interface Content : NSObject
#property (nonatomic,assign) CLLocationCoordinate2D coordinate;
// ...
#interface Annotation : NSObject <MKAnnotation, AnnotationProtocol>
-(id) initWithContent:(Content*)content;
// ...
The Annotation implements AnnotationProtocol to announce it wants to handle the creation of its own MKAnnotationView. That is, your MKMapViewDelegate should have code like this:
- (MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id<MKAnnotation>)annotation
{
// if this is a custom annotation, delegate the implementation of the view
if ([annotation conformsToProtocol:#protocol(AnnotationProtocol)]) {
return [((NSObject<AnnotationProtocol>*)annotation) annotationViewInMap:mapView];
} else {
// else, return a standard annotation view
// ...
}
}
The view returned will be of type AnnotationView, which implements AnnotationViewProtocol to announce that it wants to handle selection/deselection. Therefore, in your map view controller, the methods mapView:didSelectAnnotationView: and mapView:didDeselectAnnotationView: should delegate in a similar way to what we saw before.
When the annotation is selected, a second annotation (CalloutAnnotation) is added, which follows the same behaviour, but this time the view returned (CalloutView) is initialized from a XIB, and contains Core Graphics code (in BaseCalloutView) to animate and replicate a callout.
The initializer of the CalloutView class:
- (id)initWithAnnotation:(CalloutAnnotation*)annotation
{
NSString *identifier = NSStringFromClass([self class]);
self = [super initWithAnnotation:annotation reuseIdentifier:identifier];
if (self!=nil){
[[NSBundle mainBundle] loadNibNamed:identifier owner:self options:nil];
// prevent the tap and double tap from reaching views underneath
UITapGestureRecognizer *tapGestureRecognizer = ...
}
return self;
}
To be able to push another view controller from the callout view I used notifications.
The SO answer I linked at the top contains two complete projects implementing this code (class names may differ). I have another project using the UML above at https://github.com/j4n0/callout.
I added custom UIButton in MKAnnotationView. And on click of that button I have shown popOver with rootViewController with the view similar as you have shown above.
I know this question is from 2011 but for people who still find it in a search:
In iOS 9 you have MKAnnotationView.detailCalloutAccessoryView which entirely replaces the standard callout.

show map callout through code in iPhone

I have tried several things but am unable to solve it out.
I have 10 custom annotations on the map depending upon the area visible.
Now I have 2 buttons next and previous. Clicking on which the callout of annotation must get displayed.
i.e if i click on next buton then callout of annotation 1 will appear and when i click next again then the callout of first will hide and callout of second will appear.
I have tried out
[self.mapView selectAnnotation:self.nextSelectedAnnotationView.annotation animated:YES]
and
[self.mapView deselectAnnotation:self.selectedAnnotationView.annotation animated:YES];
But the main problem is how to get the annotation here??
I have tried NSArray* selectedAnnotations=self.mapview.annotations to get the annotations array
id annotationView =[selectedAnnotations objectAtIndex:i];
[self.mapView selectAnnotation:annotationView animated:YES];
But no luck :(
Any other way to solve my issue.??
it may help you.
NSArray *selectedAnnotations = mapView.selectedAnnotations;
for(id annotationView in selectedAnnotations) {
[mapView deselectAnnotation:[annotationView annotation] animated:NO];
}

How to open call out MKAnnotationView programmatically? (iPhone, MapKit)

I want to open up the callout for an MKPinAnnotationView programmatically. Eg I drop 10 pins on the map, and want to open up the one closest to me. How would I go about doing this?
Apple has specified the 'selected' parameter for MKAnnotationView's, but discourages setting it directly (this doesn't work, tried it).
For the rest MKAnnotationView only has a setHighlighted (same story), and can ShowCallout method..
Any hints if this is possible at all?
In your mapViewController create an action method:
- (void)openAnnotation:(id)annotation
{
//mv is the mapView
[mv selectAnnotation:annotation animated:YES];
}
You can then determine the closest annotation based on current location and walking the annotations available in the array.
[mv annotations];
Once the closest annotation is calculated, call:
[self openAnnotation:closestAnnotation];
The mapView should scroll automatically to place your annotation in the center of the display area.
In swift 3 this is updated to:
func openAnnotation(annotation: MkAnnotation) {
_ = [mapView .selectAnnotation(annotation, animated: true)]
}
and can be called using any annotation (this will open the annotation callout view and attempt to center the annotation on the map)
For example using the second annotation in a hypothetical list of annotations.
openAnnotation(annotation: mapView.annotations[1])