Ios 不显示MapKit注释
我在获取注释以在iOS应用程序的地图上显示时遇到问题 我遵守了文件中的约定。通过调试,似乎从未为注释点调用viewForAnnotation 视图控制器的代码为:Ios 不显示MapKit注释,ios,cocoa,cocoa-touch,mapkit,Ios,Cocoa,Cocoa Touch,Mapkit,我在获取注释以在iOS应用程序的地图上显示时遇到问题 我遵守了文件中的约定。通过调试,似乎从未为注释点调用viewForAnnotation 视图控制器的代码为: #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super // Do any additional setup after l
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super
// Do any additional setup after loading the view, typically from a nib.
self.mapView.delegate = self;
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager requestAlwaysAuthorization];
[self.locationManager startUpdatingLocation];
self.mapView.showsUserLocation = YES;
[self.mapView setMapType:MKMapTypeStandard];
[self.mapView setZoomEnabled:YES];
[self.mapView setScrollEnabled:YES];
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
//View Area
MKCoordinateRegion region = { { 0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = self.locationManager.location.coordinate.latitude;
region.center.longitude = self.locationManager.location.coordinate.longitude;
region.span.longitudeDelta = 0.005f;
region.span.longitudeDelta = 0.005f;
[self.mapView setRegion:region animated:YES];
[self.mapView addAnnotations:[self createAnnotations]];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
NSLog(@"viewForAnnotation");
if([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] init];
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"foo"];
[pinView setPinColor:MKPinAnnotationColorGreen];
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
return pinView;
}
- (NSMutableArray *)createAnnotations
{
NSMutableArray *annotations = [[NSMutableArray alloc] init];
CLLocationCoordinate2D coord;
coord.latitude = 37.4;
coord.longitude = -122.2;
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotations addObject:annotation];
return annotations;
}
@end
createAnnotations方法中存在一个明显的问题: 从未设置注释的坐标特性。 在创建注释后进行设置:
感谢您,问题仍然是viewForAnnotation仍然只是为用户点调用。
CLLocationCoordinate2D coord;
coord.latitude = 37.4;
coord.longitude = -122.2;
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotations addObject:annotation];
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = coord; // <-- add this line
[annotations addObject:annotation];