xcode - viewForAnnotation慢(超过3k注释)......正常吗?

时间:2013-04-27 08:48:27

标签: xcode json annotations mapkit android-mapview

Hy Guys!

我的MySQL中有大约3-4k的注释,通过同步请求接收它们...只显示注释是好的,但我希望用户可以选择突出显示的注释转到DetailView(尚未实现,它只用于测试)...

只显示Annotations是好的,只需几秒钟(没有“viewForAnnotation”方法)......但是:使用此方法大约1分钟显示所有注释并让我有可能使用我的“ showDetails“按钮。

OR:我的代码是错误的(我在Xcode中是全新的)...看起来我的代码为每个注释创建了一个完整的新视图,对吗?

如果我的代码是完整的废话告诉我,我必须从头开始尝试,但也许我可以使用代码并改变一些事情。 : - )

这是第4个帮助 格哈德

MapViewController.h

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

@interface MapViewController : UIViewController <MKMapViewDelegate>
/*{
   IBOutlet MKMapView* mapView;
}*/
@property (strong, nonatomic) IBOutlet MKMapView *mapView;
@property (strong, nonatomic) IBOutlet UIView *loadingView;
@property (nonatomic, strong) NSMutableArray *pois;
@end

MapViewController.m

#import "MapViewController.h"

@interface MapViewController ()
@end

@implementation MapViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[self navigationController] setNavigationBarHidden:NO animated:NO];

    _mapView.delegate = self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self loadPoiData];
    [self zoomToUserLocation:self.mapView.userLocation];
}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    [self zoomToUserLocation:self.mapView.userLocation];
}

- (void)zoomToUserLocation:(MKUserLocation *)userLocation
{
    if (!userLocation)
        return;

    MKCoordinateRegion region;
    region.center = userLocation.location.coordinate;
    region.span = MKCoordinateSpanMake(0.2, 0.2);
    region = [self.mapView regionThatFits:region];
    [self.mapView setRegion:region animated:YES];   
}

- (void)loadPoiData
{
    CLLocationCoordinate2D location;

    NSURL *url = [NSURL URLWithString:@"http://myURL/AppFiles/poidrive.php"];
    NSData *jsonData = [NSData dataWithContentsOfURL:url];
    if(jsonData != nil)
    {
        NSError *error = nil;
        _pois = [NSJSONSerialization JSONObjectWithData:jsonData     options:NSJSONReadingMutableContainers error:&error];
    }

    _loadingView.hidden = YES;

    for (NSDictionary *annotations in _pois)
    {
        location.latitude = [annotations[@"POI_LAT"] doubleValue];
        location.longitude = [annotations[@"POI_LONG"] doubleValue];

        CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(location.latitude, location.longitude);

        MKPointAnnotation *point = [MKPointAnnotation new];

        point.coordinate = coords;
        point.title = [annotations objectForKey:@"POI_NAME"];
        point.subtitle = [annotations objectForKey:@"POI_ID"];

        [self.mapView addAnnotation:point];
    }
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
// if it's the user location, just return nil.
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

// try to dequeue an existing pin view first
static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKPinAnnotationView* pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] ;
pinView.animatesDrop=YES;
pinView.canShowCallout=YES;
NSLog(@"test");

UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[rightButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;

//UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"profile.png"]];
//pinView.leftCalloutAccessoryView = profileIconView;

return pinView;
}

-(IBAction)showDetails:(id)sender
{
NSLog(@"%@",((UIButton*)sender).currentTitle);
}

@end

1 个答案:

答案 0 :(得分:0)

您在viewForAnnotation的每次通话中创建新视图是对的。请查看有关reusing annotation views的Apple文档,尤其是dequeueing