0

嗨伙计们!

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

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

或者:我的代码错了吗(我在 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
4

1 回答 1

0

你是对的,你正在为每次调用创建一个新视图viewForAnnotation。查看有关重用注释视图的 Apple 文档,尤其是dequeueing

于 2013-04-29T01:00:01.430 回答