【问题标题】:MapKit Annotations Not AppearingMapKit 注释未出现
【发布时间】:2015-02-04 22:46:28
【问题描述】:

我在获取注释以显示在 iOS 应用程序的地图上时遇到问题。

我已遵循文档中的约定。从调试来看,似乎从未为注释点调用 viewForAnnotation。

视图控制器的代码是:

#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

【问题讨论】:

    标签: ios cocoa cocoa-touch mapkit


    【解决方案1】:

    createAnnotations 方法中存在一个明显的问题:

    CLLocationCoordinate2D coord;
    coord.latitude = 37.4;
    coord.longitude = -122.2;
    MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
    [annotations addObject:annotation];
    


    永远不会设置 annotationcoordinate 属性。

    创建annotation后设置:

    MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
    annotation.coordinate = coord;  // <-- add this line
    [annotations addObject:annotation];
    

    【讨论】:

    • 谢谢你——问题仍然是 viewForAnnotation 仍然只为用户点调用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多