1

新的目标c!

我试图让我的地图视图缩放。我从书中的作业中复制了一些代码,但不知何故无法缩放。只显示未缩放的地图视图。

有什么建议么?请参阅下面的 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
4

1 回答 1

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对象并确保它有效。

于 2013-07-17T23:48:41.197 回答