views:

37

answers:

1

Hello all,

Is it possible to use MKPinAnnotationView controls in a application that has nothing to do with maps. I do not want to use MKMapView, I just want to reuse the MKPinAnnotationView control and be able to drop it at a specific screen coordinate.

Thanks, Cristian

A: 

Well, it derives from UIView so it seems possible in theory, but it may not behave properly if its superview isn't a MKMapView. I would try it and see what happens (and let the rest of us know).

Alternatively, it wouldn't be too hard to create your own UIView subclass that behaves very similarly to the MKPinAnnotationView. (I would probably subclass UIControl since you probably want to be able to respond to user events.)

EDIT: I just tried a very basic implementation and it seems to work fine. Here's the code I used in my view controller. MyAnnotation is a very simple class that implements the MKAnnotation protocol.

- (void)viewDidLoad {
    [super viewDidLoad];

    MyAnnotation *annotation = [[[MyAnnotation alloc] init] autorelease];

    MKPinAnnotationView *pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil] autorelease];
    pinView.center = self.view.center;
    [self.view addSubview:pinView];
}

Another update: Well, the pin displays, but it's not draggable even if draggable = YES, and the callout doesn't appear even if canShowCallout = YES. This makes sense given that these behaviors are actually controlled by the map view, and not the actual pin view.

So if you just want to display a pin with a shadow on your view, it works great. If you want some of the functionality you get when the pin is displayed in a map view, you have to implment it yourself in your view or view controller.

Robot K
You don't need to subclass `UIControl` to respond to touch events.
rpetrich
True, but it's a lot easier to respond to control events than manage touches yourself.
Robot K
Thank you for the answer, I was hopping that it could be done by implementing MKMapViewDelegate or something like that. I'm more of a C/C++ programmer and all of this objective-c stuff scares me a little. The pin also had some animations that looked cool, which I think may be difficult to be done by me.
Cristi