ios - MKPinAnnotationView 不起作用
<p><p>我想要一个 <code>MKMapView</code> 显示带有公开按钮的注释,这些注释会导致像 <a href="https://developer.apple.com/library/ios/#samplecode/MapCallouts/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009746" rel="noreferrer noopener nofollow">the Golden Gate Bridge annotation in this Apple sample app</a> 这样的 ViewController .</p>
<p>我从 plist 加载坐标,注释正确显示为标题/副标题,但方法</p>
<pre><code>- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
</code></pre>
<p>没有效果。</p>
<p>我想我必须以某种方式将注释与 pinannotations 联系起来?</p>
<p>MapViewController.h:</p>
<pre><code>#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "Annotation.h"
@interface MapViewController : UIViewController<CLLocationManagerDelegate, MKMapViewDelegate>
@property (strong, nonatomic) CLLocationManager *location;
@property (nonatomic, retain) NSArray *data;
@end
</code></pre>
<p>MapViewController.m:</p>
<pre><code>#import "MapViewController.h"
@interface MapViewController ()
@property (nonatomic, weak) IBOutlet MKMapView *mapView;
@end
@implementation MapViewController
@synthesize data;
@synthesize location, minLatitude, maxLatitude, minLongitude, maxLongitude;
- (void)viewDidLoad
{
NSString *dataPath = [ pathForResource:@"City" ofType:@"plist"];
self.data = ;
for (int i = 0; i < data.count; i++) {
NSDictionary *dataItem = ;
//Create Annotation
Annotation *building = [ init];
building.title = ;
building.subtitle = ;
MKCoordinateRegion buildingcoordinates = { {0.0, 0.0}, {0.0, 0.0} };
buildingcoordinates.center.latitude = [ floatValue];
buildingcoordinates.center.longitude = [ floatValue];
building.coordinate = buildingcoordinates.center;
;
}
;
}
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if (])
return nil;
static NSString *pinIdentifier = @"pinIndentifier";
MKPinAnnotationView *pinView = (MKPinAnnotationView *)
;
if (pinView == nil)
{
// if an existing pin view was not available, create one
MKPinAnnotationView *customPinView = [
initWithAnnotation:annotation reuseIdentifier:pinIdentifier];
customPinView.pinColor = MKPinAnnotationColorPurple;
customPinView.animatesDrop = YES;
customPinView.canShowCallout = YES;
UIButton* rightButton = ;
[rightButton addTarget:self
action:@selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
customPinView.rightCalloutAccessoryView = rightButton;
return customPinView;
}
else
{
pinView.annotation = annotation;
}
return pinView;
}
</code></pre>
<p>Annotation.h:</p>
<pre><code>#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>
@interface Annotation : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
@property(nonatomic, assign) CLLocationCoordinate2D coordinate;
@property(nonatomic, copy) NSString *title;
@property(nonatomic, copy) NSString *subtitle;
@end
</code></pre>
<p>Annotation.m:</p>
<pre><code>#import "Annotation.h"
@implementation Annotation
@synthesize coordinate, title, subtitle;
@end
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>很可能 mapView 的 <code>delegate</code> 没有设置,这意味着 <code>viewForAnnotation</code> 委托(delegate)方法将不会被调用。</p>
<p>由于您已将 <code>mapView</code> 声明为 IBOutlet,因此在 xib 中,请确保 mapView 的 <code>delegate</code> 已连接到 File's Owner。</p>
<p>或者,在 MapViewController 中 <code>viewDidLoad</code> 方法的顶部,以编程方式设置它:</p>
<pre><code>mapView.delegate = self;
</code></pre></p>
<p style="font-size: 20px;">关于ios - MKPinAnnotationView 不起作用,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/14616622/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/14616622/
</a>
</p>
页:
[1]