异步加载MKMapView?

发布于 2024-09-28 07:25:22 字数 2798 浏览 0 评论 0原文

当我第一次打开 MKMapView 时,我的应用程序在加载时似乎暂停了。我假设这是因为它不异步加载。

有什么办法可以解决这个问题吗?当它加载时,您无法切换选项卡或执行任何操作。

谢谢

编辑:这是我的 viewDidLoad 方法:

CGRect bounds = self.view.bounds;
    mapView = [[MKMapView alloc] initWithFrame:bounds];
    mapView.mapType=MKMapTypeStandard;
    mapView.showsUserLocation = YES;
    mapView.delegate = self;

    [self.view insertSubview:mapView atIndex:0];

    NSArray *mapTypes = [NSArray arrayWithObjects:@"Standard",@"Satellite",@"Hybrid",nil];

    mapType = [[UISegmentedControl alloc] initWithItems:mapTypes];
    [mapType setSegmentedControlStyle:UISegmentedControlStyleBar];
    [mapType setCenter:CGPointMake(210, 345)];
    [mapType setSelectedSegmentIndex:0];
    [mapType setTintColor:[UIColor darkGrayColor]];
    [mapType addTarget:self action:@selector(changeType:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:mapType];

    // refresh button...
    UISegmentedControl *refreshButton = [[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Refresh",nil]] autorelease];
    [refreshButton setSegmentedControlStyle:UISegmentedControlStyleBar];
    [refreshButton setCenter:CGPointMake(self.view.frame.size.width-((refreshButton.frame.size.width/2)+5),(refreshButton.frame.size.height/2)+5)];
    [refreshButton setMomentary:YES];
    [refreshButton setTintColor:[UIColor darkGrayColor]];
    [refreshButton addTarget:self action:@selector(updateMarkers) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:refreshButton];


    UIBarButtonItem *leftBarButton = [[[UIBarButtonItem alloc] initWithTitle:@"List" style:UIBarButtonItemStylePlain target:self action:@selector(goToList)] autorelease];
    [self.navigationItem setRightBarButtonItem:leftBarButton animated:YES];

    // refresh label
    refreshView = [[UIView alloc] initWithFrame:CGRectMake(5, 5, self.view.frame.size.width-74, 40)];
    [refreshView setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.8]];
    [refreshView.layer setCornerRadius:5];
    [self.view addSubview:refreshView];

    UILabel *refreshLabel = [[[UILabel alloc] initWithFrame:CGRectMake(5, 5, refreshView.frame.size.width-10, refreshView.frame.size.height-10)] autorelease];
    [refreshLabel setTextColor:[UIColor whiteColor]];
    [refreshLabel setBackgroundColor:[UIColor clearColor]];
    [refreshLabel setFont:[UIFont fontWithName:@"Helvetica" size:12]];
    [refreshLabel setNumberOfLines:2];
    [refreshLabel setLineBreakMode:UILineBreakModeWordWrap];
    [refreshLabel setText:@"Move the map and press 'refresh' to load new results."];
    [refreshView addSubview:refreshLabel];

    [NSTimer scheduledTimerWithTimeInterval:10 target:refreshView selector:@selector(removeFromSuperview) userInfo:nil repeats:NO];

When I first bring up an MKMapView my app seems to pause while it loads. I am assuming this is because it doesn't load asynchronously.

Is there any way I can get around this. While it loads you can't switch tabs or do anything.

Thanks

EDIT: here is my viewDidLoad method:

CGRect bounds = self.view.bounds;
    mapView = [[MKMapView alloc] initWithFrame:bounds];
    mapView.mapType=MKMapTypeStandard;
    mapView.showsUserLocation = YES;
    mapView.delegate = self;

    [self.view insertSubview:mapView atIndex:0];

    NSArray *mapTypes = [NSArray arrayWithObjects:@"Standard",@"Satellite",@"Hybrid",nil];

    mapType = [[UISegmentedControl alloc] initWithItems:mapTypes];
    [mapType setSegmentedControlStyle:UISegmentedControlStyleBar];
    [mapType setCenter:CGPointMake(210, 345)];
    [mapType setSelectedSegmentIndex:0];
    [mapType setTintColor:[UIColor darkGrayColor]];
    [mapType addTarget:self action:@selector(changeType:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:mapType];

    // refresh button...
    UISegmentedControl *refreshButton = [[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Refresh",nil]] autorelease];
    [refreshButton setSegmentedControlStyle:UISegmentedControlStyleBar];
    [refreshButton setCenter:CGPointMake(self.view.frame.size.width-((refreshButton.frame.size.width/2)+5),(refreshButton.frame.size.height/2)+5)];
    [refreshButton setMomentary:YES];
    [refreshButton setTintColor:[UIColor darkGrayColor]];
    [refreshButton addTarget:self action:@selector(updateMarkers) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:refreshButton];


    UIBarButtonItem *leftBarButton = [[[UIBarButtonItem alloc] initWithTitle:@"List" style:UIBarButtonItemStylePlain target:self action:@selector(goToList)] autorelease];
    [self.navigationItem setRightBarButtonItem:leftBarButton animated:YES];

    // refresh label
    refreshView = [[UIView alloc] initWithFrame:CGRectMake(5, 5, self.view.frame.size.width-74, 40)];
    [refreshView setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.8]];
    [refreshView.layer setCornerRadius:5];
    [self.view addSubview:refreshView];

    UILabel *refreshLabel = [[[UILabel alloc] initWithFrame:CGRectMake(5, 5, refreshView.frame.size.width-10, refreshView.frame.size.height-10)] autorelease];
    [refreshLabel setTextColor:[UIColor whiteColor]];
    [refreshLabel setBackgroundColor:[UIColor clearColor]];
    [refreshLabel setFont:[UIFont fontWithName:@"Helvetica" size:12]];
    [refreshLabel setNumberOfLines:2];
    [refreshLabel setLineBreakMode:UILineBreakModeWordWrap];
    [refreshLabel setText:@"Move the map and press 'refresh' to load new results."];
    [refreshView addSubview:refreshLabel];

    [NSTimer scheduledTimerWithTimeInterval:10 target:refreshView selector:@selector(removeFromSuperview) userInfo:nil repeats:NO];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文