0

我想为应用程序设置一个默认区域,每次打开地图时它都应该显示一个区域。例如,每次我打开地图时,它都应该显示“伦敦”。

我这样做了,但它没有居中

@interface AppleMapViewController ()

@end

@implementation TicinoWineAppleMapViewController
@synthesize mapView = _mapView;

#define DEFAULT_LATITUDE 46.006512
#define DEFAULT_LONGITUDE 8.952312

#define THE_SPAN 10.0f


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

    }
    return self;
}


- (void)viewWillAppear:(BOOL)animated
{
    NSLog(@"ViewWillAppear");

}

- (void)createDefaultRegion
{
    MKCoordinateRegion defaultRegion;

    CLLocationCoordinate2D center;
    center.latitude = DEFAULT_LATITUDE;
    center.longitude = DEFAULT_LONGITUDE;

    MKCoordinateSpan span;
    span.latitudeDelta = THE_SPAN;
    span.longitudeDelta = THE_SPAN;

  //  defaultRegion.center = center;
  //  defaultRegion.span = span;

    defaultRegion = MKCoordinateRegionMake(center, span);

    TicinoWineMapViewAnnotation *annotation = [[TicinoWineMapViewAnnotation alloc]initWithTitle:@"ciao" andCoordinate:center];
    [_mapView addAnnotation:annotation];


    [_mapView setRegion:defaultRegion animated:YES];
    [_mapView setCenterCoordinate:center animated:YES];

}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self createDefaultRegion];


}


@end

我尝试了两种方法:

    [_mapView setRegion:defaultRegion animated:YES];
    [_mapView setCenterCoordinate:center animated:YES];

但它并不以我想要的为中心。这可能是模拟器的问题?

4

1 回答 1

0

你可以试试这个功能。它对我有用。

  • (void)recenterMap
    {

    NSArray *coordinates = [Mapsview valueForKeyPath:@"annotations.coordinate"];

    CLLocationCoordinate2D maxCoord = {-90.0f, -180.0f};
    CLLocationCoordinate2D minCoord = {90.0f, 180.0f};

    for(NSValue *坐标值) {

    CLLocationCoordinate2D coord = {0.0f, 0.0f};
    
    [value getValue:&coord];
    
    if(coord.longitude > maxCoord.longitude) {            
        maxCoord.longitude = coord.longitude;            
    }        
    if(coord.latitude > maxCoord.latitude) {            
        maxCoord.latitude = coord.latitude;            
    }        
    if(coord.longitude < minCoord.longitude) {            
        minCoord.longitude = coord.longitude;            
    }        
    if(coord.latitude < minCoord.latitude) {            
        minCoord.latitude = coord.latitude;
    }        
    

    }

    MKCoordinateRegion 区域 = {{0.0f, 0.0f}, {0.0f, 0.0f}};

    //region.center.longitude = (minCoord.longitude + maxCoord.longitude) / 2.0; // region.center.latitude = (minCoord.latitude + maxCoord.latitude) / 2.0;

    region.center.longitude = minCoord.longitude + (maxCoord.longitude - minCoord.longitude)/2.0f; region.center.latitude = minCoord.latitude + (maxCoord.latitude - minCoord.latitude)/2.0f;

    region.span.longitudeDelta = (maxCoord.longitude - minCoord.longitude) * 1.5; region.span.latitudeDelta = (maxCoord.latitude - minCoord.latitude) * 1.5;

    [Mapsview setRegion:区域动画:YES];

}

于 2013-09-19T10:23:30.737 回答