I have an MKAnnotationView subclass called ImageAnnotationView. It basically displays an image on the map. I want the regular MKAnnotationView views (the default pins) to appear above the ImageAnnotationView views.
This is what I've tried so far but it doesn't seem to work:
- (void)mapView:(MKMapView *)imapView didAddAnnotationViews:(NSArray *)views {
for (MKAnnotationView *annView in views) {
if ( [annView isKindOfClass:[ImageAnnotationView class]] ) {
[imapView sendSubviewToBack:annView];
}
}
}
Am I doing this in the wrong place? Are the MKAnnotationViews all direct subviews of MKMapView or is there some hidden hierarchy?
Any ideas or help are welcome.
it should work, im doing same just in MKAnnotationView subclass [[self superview] sendSubviewToBack:self];
Related
I have a MKMap with a series of MKAnnotations, all of which are red which is fine. I have selected "show user location" in IB and to change the MKAnnotation from red to blue, I have the code in my viewForAnnotation method:
if (annotation == theMap.userLocation)
return nil;
All is good and the app works fine, but if the user inadvertently taps the blue userlocation dot I get the following crash:
2012-02-01 20:43:47.527 AusReefNSW[27178:11603] -[MKUserLocationView setPinColor:]: unrecognized selector sent to instance 0x79b0720
2012-02-01 20:43:47.528 AusReefNSW[27178:11603] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MKUserLocationView setPinColor:]: unrecognized selector sent to instance 0x79b0720'
*** First throw call stack:
If I remove the above code, all works well but the pin is red. I perfer to have the blue icon but as yet have not discovered why the crash. Any ideas would be appreciated. Thanks.
SOLVED! Thanks Marvin and heres the code incase anyone finds it useful. In a nutshell, I had to first check to see if the MKAnnotation was of MyAnnotation Class or of MKUserLocation Class.
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKPinAnnotationView *)view
{
theAnnotationSelected = [[mapView selectedAnnotations] objectAtIndex:0];
if ([theAnnotationSelected isKindOfClass:[MyAnnotation class]] )
{
view.pinColor = MKPinAnnotationColorGreen;
}
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKPinAnnotationView *)view
{
if ([theAnnotationSelected isKindOfClass:[MyAnnotation class]] )
{
view.pinColor = MKPinAnnotationColorRed;
}
For Current User Location go to MKMapView property and make selection on Shows User Location in XIB.
then Implement MKMapViewDelegate in your controller..and write this method in your controller
-(MKAnnotationView *)mapView:(MKMapView *)pmapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil; //return nil to use default blue dot view
if([annotation isKindOfClass:[MKAnnotationClass class]])
{
//Your code
}
}
and this
- (void)mapView:(MKMapView *)mapView1 didSelectAnnotationView:(MKAnnotationView *)customAnnotationView
{
if(![annotation isKindOfClass:[MKUserLocation class]])
{
//Your code
}
}
Using this you can see blue dot on User Location..
You will want to check the class of the annotation in
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
Then do the following..
id *annotation = view.annotation;
if (![annotation isKindOfClass:[MKUserLocation class]]) {
//Normal Code here
}
This solution works in iOS7 to suppress the callout view, but it does not stop the user from selecting the blue dot. How to suppress the "Current Location" callout in map view
I have yet to find a way to disable selection of the current location dot.
It isn't so much a workaround as a "way to deal with it", but I zoom the map in to focus on certain types of annotations when tapped. If the user happens to be located right on top of one, the zooming helps put some distance between the blue dot and my annotation, making it easier to tap.
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
if([view.annotation isKindOfClass:[SpecialClass class]]){
SpecialClass *cluster = (SpecialClass *)view.annotation;
if(testCriteria){
[self.mapView setRegion:MKCoordinateRegionMakeWithDistance(cluster.coordinate, cluster.radius, cluster.radius) animated:YES];
}
}
I had the same issue such that when a user would tap the MKUserlocation "blue dot" the application would crash. I am using DDAnnotation by Ching-Lan 'digdog' HUANG. The fix I found was
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
id annotation = view.annotation;
if (![annotation isKindOfClass:[MKUserLocation class]]) {
//Your Annotation Code here
}
}
I'm trying to put a little button in my AnnotationViews to show some info about some places on a map. Sounds super basic? Well. I must be an idiot then...
I've looked at multiple examples, searched the whole internet, and I have ripped of 95% of my hair. It just doesn't work!
-(MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)theAnnotation;
{
MKAnnotationView * annotationView = nil;
if([theAnnotation class] == [MyAnnotation class])
{
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:theAnnotation reuseIdentifier:#"annotation"];
annotationView.canShowCallout = YES;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annotationView.enabled = YES;
}
return annotationView;
}
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;
{
NSLog(#"push %#", control);
}
Can some one please tell me why calloutAccessoryControlTapped never gets called?
I get a nice and pretty annotation, but when I tap the blue button it just closes. I have tried adding a target to the button, nothing.
Please help me!
PS: I know I should reuse old annotation view and stuff, but i stripped all code that wasn't necessary to be sure that there wasn't something else that was screwing thins up.
Make sure you have correctly set the delegate responding to the MKMapViewDelegate Protocol.
Also, make sure that all the views and superviews have userInterActionEnabled set to YES.
I want to navigate to next class through tap on mkpinannotationview. How can i do this plz give some sample code for this.
Thanks sandy
This mapView delegate will be called when the pin is selected.
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
// Here push your view controller
}
I have implemented one map application in which i have display pin animation for current location.When i click on the pin at that time annotation view will open. But i want to display
annotation view without clicking on pin.Is it possible if possible then please give me idea about that.
Thanks in advance.
Try out:
It seems that [mapView selectAnnotation:annotation animated:YES] works too.
Hope this helps.
You just need to find your MKAnnotationView instance you want to select and call -setSelected:animated:.
For example, you can loop your annotations of an MKMapView like this:
for (YOURCLASSHERE *a in mapView.annotations) {
// Your current position (if shown) is an annotation as well. Thus check for the right class!
if ([a isKindOfClass:[YOURCLASSHERE class]]) {
// insert some smart if statement here, if you have multiple annotations!
[[mapView viewForAnnotation:a] setSelected:YES animated:YES];
}
}
YOURCLASSHERE is your class which implements the MKAnnotation protocol.
Of course the loop is superfluous if you already know your annotationView.
We can show the annotation with out clicking on it. Use the following code:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
static NSString *identifier = #"Pin";
MKPinAnnotationView *pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
pin.canShowCallout = YES;
[pin setSelected:YES animated:NO];
return [pin autorelease];
}
I want the annotations callout to popup when the pin has finished it's drop animation. Currently I am able to simulate it with the following method:
- (void)showCallOut {
[myMapView selectAnnotation:[myMapView.annotations objectAtIndex:0] animated:YES];
}
In my viewDidLoad is where my annotation is created
[myMapView addAnnotation:annotation];
The problem is that you simply can't callout [self showCallOut]; after that because at run time it responds before MapKit has "acknowledged" the annotation drop. I need to either create a delay (Would like to avoid this) or find the proper way to detect when annotations are in place and then run the showCallOut method.
Thanks for any help!
Thanks to aBitObvious below for providing a solution:
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
[self performSelector:#selector(showCallOut) withObject:nil afterDelay:1];
}
Try using the didAddAnnotationViews delegate method:
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
[self showCallOut];
}
Make sure your map view's delegate is set.
Edit:
If you need to add a delay regardless, then try this instead (example with 1/2 second delay):
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
[self performSelector:#selector(showCallOut) withObject:nil afterDelay:0.5];
}