1

The location is working but the title isn't appearing, most strange.

location.latitude = (double) 51.501468;
location.longitude = (double) -0.141596;

// Add the annotation to our map view
MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:@"ABC" andCoordinate:location];
[self.mapView addAnnotation:newAnnotation];
// [newAnnotation release];

MapViewAnnotation.h

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

@interface MapViewAnnotation : NSObject <MKAnnotation> {

NSString *title;
CLLocationCoordinate2D coordinate;

}

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

- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d;

@end

and MapViewAnnotation.m

 #import "MapViewAnnotation.h"

 @implementation MapViewAnnotation

 @synthesize title, coordinate;

 - (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d {
title = ttl;
coordinate = c2d;
return self;
 }

 @end

[newAnnotation release] is remmed out to keep the ARC happy :-) Any ideas?

4

3 回答 3

1

这成功了:

[mapView selectAnnotation:newAnnotation animated:YES];

以前只有在您单击 Pin 图时才会显示标题。

于 2012-11-09T17:50:00.533 回答
0

该代码看起来不错(该方法的非标准实现除外init)。

标题(标注)未出现的最可能原因是在您的viewForAnnotation委托方法中,您未设置canShowCalloutYES(默认为NO)。

viewForAnnotation委托方法中,创建后MKAnnotationView,设置canShowCalloutYES.


与未显示的标题/标注无关init,您的MapViewAnnotation类中的方法应该这样实现:

- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d {
    self = [super init];
    if (self) {
        title = ttl;
        coordinate = c2d;
    }
    return self;
}
于 2012-11-08T13:58:56.287 回答
0

你打电话给注释代表参考这个链接mapkit-example-in-iphone

于 2012-11-08T12:00:28.893 回答