为什么不放大?

时间:2013-07-17 23:42:51

标签: ios objective-c

目标c的新内容!

我试图让我的mapview变焦。我已经从我的书中的作业中复制了一些代码,但它不知何故不能缩放。只是显示未经修饰的mapview。

有什么建议吗?请参阅下面的viewController代码。

另外 - 关于导入头文件和@class指令的区别的几句话会很棒!?

提前致谢

#import "TrackViewController.h"
#import "MainWindowViewController.h"
#import <MapKit/MapKit.h>



@class MainWindowViewController;
@implementation TrackViewController

- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{    
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if(self)
{
    locationManager = [[CLLocationManager alloc]init];
    [locationManager setDelegate:self];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
}
    return self;
}

-(IBAction) back:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void) findLocation;
{
    [locationManager startUpdatingLocation];
}

-(void) foundLocation:(CLLocation *)loc
{
    CLLocationCoordinate2D coord = [loc coordinate];
    MKCoordinateRegion region =  MKCoordinateRegionMakeWithDistance(coord, 100, 100);
    [worldView setRegion:region];
    [locationManager stopUpdatingLocation];
}

-(void)viewDidLoad
{
    [worldView setShowsUserLocation:YES];
    [worldView setMapType:MKMapTypeHybrid];    
}

-(void)dealloc
{
    [locationManager setDelegate:nil];
}

- (void) locationManager:(CLLocationManager *) manager
 didUpdateToLocation:(CLLocation *)newLocation
        fromLocation:(CLLocation *)oldLocation
{

NSTimeInterval t = [[newLocation timestamp] timeIntervalSinceNow];
if(t<180){
    return;
}
    [self foundLocation:newLocation];
}

-(void)locationManager:(CLLocationManager *)manager
  didFailWithError:(NSError *)error
{
    NSLog(@"Could not find location: %@", error);
}

@end

1 个答案:

答案 0 :(得分:2)

我唯一注意到的是将setRegion行更改为:

[worldView setRegion:region animated:YES];//instead of just setRegion

- setRegion:文档声明:

The area currently displayed by the map view

- 虽然setRegion:animated:文档说明了:

Changes the currently visible region and optionally animates the change

否则,我建议您打印CLLocationCoordinate2D对象并确保其有效。