show map callout through code in iPhone - 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];
}

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];
}

SetUserTrackingMode not working

I am programming a map app on iPhone and want the map to rotate as the user changes his direction. I have read most of the posts on stackoverflow. Most of them suggest the use of setUserTrackingMode with MKUserTrackingModeFollowWithHeading if we are working with iOS 5 or later. This does not seem to work with me for some reason. Following is my code:
-(IBAction)getLocation //This is a button
{
mapView.showsUserLocation=YES; //mapView is the instance of MKMapView
[mapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:YES];
}
This only shows the user location but if I move the phone, it doesn't rotate.
One more thing is, I downloaded a project from internet, and I included this line. It worked there only for the first time. I have no idea why this is happening.
Any suggestions?
You need to wait for the 'MapView' finish loading...
follow:
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView {
mapView.userTrackingMode = MKUserTrackingModeFollow;
}
follow & heading:
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView {
mapView.userTrackingMode = MKUserTrackingModeFollowWithHeading;
}
The easier way to do this is to include an MKUserTrackingBarButtonItem instead of creating your own button. It acts exactly the same as the button in the iOS 5 Maps app and is easy to set up.
Here's how to use it:
// You should have an outlet to your map view called mapView
MKUserTrackingBarButtonItem *userTrackingButton;
userTrackingButton = [[MKUserTrackingBarButtonItem alloc] initWithMapView:self.mapView];
// You need an outlet to your toolbar too
[self.toolbar setItems:[NSArray arrayWithObject:userTrackingButton]];
Change "Animated" to "animated" and try again

Remove MKMapView Overlay on Button Push

I have a MKMapView with a MKOverlay over it showing the location history of the user. On a button press, how can I discard this overlay and remove it from view?
I have tried [map removeOverlay:overlay]; but that doesn't work - it still shows.
This will work
NSArray *pointsArray = [mapView overlays];
[mapView removeOverlays:pointsArray];
Just to add that, for my iPad application, I needed to add an extra line to the solution shown above:
NSArray *pointsArray = [self.mapView overlays];
[self.mapView removeOverlays:pointsArray];
self.mapOverlayView = nil;
Without setting mapOverlayView to nil, the "removeOverlays" call didn't seem to do much (?)

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

Get info on a mapview selected annotation

I have annotations on a mapview and a callout with a button on each. What I need to do is grab properties from this callout, ie. the title, but logging this line:
NSLog(#"%#", mapView.selectedAnnotations);
returns <AddressAnnotation: 0x1bdc60> which obviously gives me no useful info...
My question is, how can I access the properties of a selected annotation callout?
This is how
for (id annotation in mapView.annotations) {NSLog([annotation title]);}
Here is what I did in the annotationviewClick function:
Hope this helps
-(IBAction) annotationViewClick:(id) sender{
[self.view addSubview:LoadingView];
Annotation *ann = [myMap.selectedAnnotations objectAtIndex:([myMap.selectedAnnotations count]-1)];
NSLog(#"Selected:%#", [ann tag]);
}
mapView.selectedAnnotations returns an array of anotations. You should access its items to get info.