在用户点击上显示MapPin标题

时间:2015-12-09 05:08:33

标签: ios objective-c

我已经制作了像这样的MapPin课程

//.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface MapPin : NSObject <MKAnnotation> {
    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
}

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;

@end

AND

.m
#import "MapPin.h"

@implementation MapPin

@synthesize coordinate,title,subtitle;

@end

然后在我班级的cellForRowAtIndexPath中,我有

MapPin *ann = [[MapPin alloc] init];
    ann.title = @"HERE";
    ann.subtitle = @"Test";
    ann.coordinate = region.center;
    [riCell.mapView addAnnotation:ann];

当用户点击它时,我想要显示标题和副标题?是否有像MapPin那样的方法?我虽然应该自动发生?

如何在用户点击中显示它?

由于

2 个答案:

答案 0 :(得分:0)

您可以使用CLGeocoder委托方法并使用CLPlacemark对象并获取位置并相应地分发到注释

// get the place details from the current location
[geocoder reverseGeocodeLocation:self.locationManager.location
               completionHandler:^(NSArray *placemarks, NSError *error) {

                   if (error){
                       NSLog(@"Geocode failed with error: %@", error);
                       return;
                    }

                   // get all the details of the current location
                   placemark = [placemarks objectAtIndex:0];

                   // set the place details to the annotation
                   annotation.title = [NSString stringWithString:_placemark.subLocality];
                   annotation.subtitle = [NSString stringWithString:_placemark.locality];
                   [self.mapView addAnnotation:annot];


                }];
}

答案 1 :(得分:0)

这是Swift代码......您需要在Objective C中实现

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        /*
            For now, all we have are some quick and dirty pins for viewing debug
            annotations. To learn more about showing annotations, 
            see "Annotating Maps".
        */

        if (annotation.title! == "red") {
            let pinView = MKPinAnnotationView()
            if #available(iOS 9.0, *) {
                pinView.pinTintColor = UIColor.redColor()
            } else {
                pinView.pinColor = MKPinAnnotationColor.Red
            }
            pinView.canShowCallout = true
            pinView.draggable = true
            return pinView
        }

canShowCallout = true是这里的关键