gpt4 book ai didi

iphone - 优化 MKMapView 代码——大量注解

转载 作者:可可西里 更新时间:2023-11-01 03:07:01 25 4
gpt4 key购买 nike

我的应用程序中有一个显示 UIMapView 的模态视图。然后我向这个 map View (下面的代码)添加了大量注释(超过 800 个)。

问题是在加载所有图钉时,用户被迫等待一分钟左右。此外,一旦所有 800 个图钉都在 map 上,应用程序就会变得缓慢。

谁能建议我如何改进下面的代码?

谢谢。

#import "MapView.h"
#import "MapPlaceObject.h"


@implementation MapView
@synthesize mapViewLink, mapLocations, detail, failedLoad;

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

-(void)addPins
{

for (MapPlaceObject * info in mapLocations) {


double latitude = info.longitude;
double longitude = info.latitude;

NSString * name = info.name;
NSString * addressline = info.addressOne;
NSString * postcode = info.postCode;

NSString * addresscomma = [addressline stringByAppendingString:@", "];
NSString * address = [addresscomma stringByAppendingString:postcode];

CLLocationCoordinate2D coordinate;
coordinate.latitude = latitude;
coordinate.longitude = longitude;
MyLocation *annotation = [[[MyLocation alloc] initWithName:name address:address coordinate:coordinate] autorelease];


[mapViewLink addAnnotation:annotation];

}
}

- (void)showLinks : (id)sender {


if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
detail = [[DetailViewController alloc] initWithNibName:@"DetailViewController-iPad" bundle:nil];
}

else if (!detail) {

NSLog(@"Detail is None");

detail = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];

}

int uniqueID = ((UIButton *)sender).tag;

//PlaceObject *info = [mapLocations objectAtIndex:uniqueID];

detail.UniqueID = uniqueID;
detail.hidesBottomBarWhenPushed = YES;

[self.navigationController pushViewController:detail animated:YES];

self.detail = nil;

[detail release];

}

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{

if (annotation == mapView.userLocation){
return nil; //default to blue dot
}

MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
annView.pinColor = MKPinAnnotationColorRed;

nameSaved = annotation.title;

for (PlaceObject * info in mapLocations) {

if (info.name == nameSaved) {

saveID = info.UniqueID;

}
}

UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
advertButton.frame = CGRectMake(0, 0, 23, 23);
advertButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
advertButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

[advertButton addTarget:self action:@selector(showLinks:) forControlEvents:UIControlEventTouchUpInside];

advertButton.tag = saveID;

annView.rightCalloutAccessoryView = advertButton;

annView.animatesDrop=TRUE;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
return annView;

}

- (void)dealloc
{
[mapViewLink release];
[mapLocations release];
[detail release];
self.failedLoad = nil;
[failedLoad release];
[super dealloc];
}

- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

- (void)viewWillAppear:(BOOL)animated {

if (firstTime) {

CLLocationCoordinate2D zoomLocation;

zoomLocation.latitude = 51.50801;
zoomLocation.longitude = -0.12789;

MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 15*METERS_PER_MILE, 15*METERS_PER_MILE);

MKCoordinateRegion adjustedRegion = [mapViewLink regionThatFits:viewRegion];

[mapViewLink setRegion:adjustedRegion animated:YES];

firstTime = NO;

}
}

- (void)viewDidLoad
{
[super viewDidLoad];

firstTime = YES;

failedLoad = [[NSMutableArray alloc]init];

self.mapLocations = [BluePlaqueDatabase database].mapInfo;

[self addPins];
}

- (void)viewDidUnload
{
[mapViewLink release];
mapViewLink = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

最佳答案

您可以在这里做出的两个最大的速度改进是:

  • 实现注释 View 的重用(现在它每次需要显示注释时都会创建一个新 View ,即使同一个 View 再次出现也是如此。
  • 更改 UniqueID 的设置方式。要设置它,代码当前会在每次创建注释 View 时循环遍历所有注释(这可能在 map View 缩放或滚动时发生,而不仅仅是初始时间)。

首先,不是在 viewForAnnotation 方法中搜索 UniqueID 并使用按钮标记来传递注释标识符,而是 添加 UniqueID 作为自定义注释类的属性 MyLocation 并在 addPins 中添加注释本身时设置属性:

annotation.uniqueID = info.UniqueID;  // <-- give id to annotation itself
[mapViewLink addAnnotation:annotation];

您还可以将 uniqueID 作为参数添加到 initWithName 方法,而不是单独分配属性。


接下来,要实现注释 View 的重用,viewForAnnotation 方法应该如下所示:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{

if (annotation == mapView.userLocation){
return nil; //default to blue dot
}

NSString *reuseId = @"StandardPin";
MKPinAnnotationView *annView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (annView == nil)
{
annView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId] autorelease];

annView.pinColor = MKPinAnnotationColorRed;
annView.animatesDrop = YES;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);

UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
advertButton.frame = CGRectMake(0, 0, 23, 23);
advertButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
advertButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

annView.rightCalloutAccessoryView = advertButton;
}
else
{
//update the annotation property if view is being re-used...
annView.annotation = annotation;
}

return annView;
}


最后,要响应按钮按下并确定要显示哪个 UniqueID 的详细信息,请实现 calloutAccessoryControlTapped 委托(delegate)方法:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
calloutAccessoryControlTapped:(UIControl *)control
{
MyLocation *myLoc = (MyLocation *)view.annotation;

int uniqueID = myLoc.uniqueID;

NSLog(@"calloutAccessoryControlTapped, uid = %d", uniqueID);

//create, init, and show the detail view controller here...
}


在所有这些更改之后,只有注解的初始加载会占用大部分时间。如果这仍然是一个问题,一种解决方案是仅添加在当前显示区域中可见的注释,并在用户更改可见区域时添加/删除注释。

关于iphone - 优化 MKMapView 代码——大量注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7921106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com