2012-12-09 2 views
1

지도를 특정 지역의 중심에 배치하는 데 문제가 있습니다. 문제가 iOS6에서 지역을 중앙에 배치하는 방법

#import "Map.h" 
#import "Annotation.h" 


@interface Map() 

@end 

//define coordinates 

#define Sofia_LATITUDE 42.745826; 
#define Sofia_LONGITUDE 23.270186; 

#define Plovdiv_LATITUDE 42.1333; 
#define Plovdiv_LONGITUDE 24.75; 

#define Varna_LATITUDE 43.2; 
#define Varna_LONGITUDE 27.9167; 

#define THE_SPAN 3.8; 

@implementation Map 

@synthesize mapView; 

- (IBAction)getLocation:(id)sender { 
    mapView.showsUserLocation = YES; 
    [self.view addSubview:mapView]; 

} 

- (IBAction)setMap:(id)sender { 
    switch (((UISegmentedControl *)sender).selectedSegmentIndex) { 
     case 0: 
      mapView.mapType = MKMapTypeStandard; 
      break; 
     case 1: 
      mapView.mapType = MKMapTypeSatellite; 
      break; 
     case 2: 
      mapView.mapType = MKMapTypeHybrid; 
      break; 
     default: 
      break; 

    } 
} 

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

- (void)viewDidLoad 
{ 

    [super viewDidLoad]; 


    MKCoordinateRegion myRegion; 

    //Center the map 



    CLLocationCoordinate2D center; 
    center.latitude = 42.1333; 
    center.longitude = 24.75; 

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

    myRegion.center = center; 
    myRegion.span = span; 

    [mapView setRegion:myRegion animated:TRUE]; 

    NSMutableArray * locations = [[NSMutableArray alloc]init]; 
    CLLocationCoordinate2D location; 
    Annotation * myAnn; 

    //make annotations 

    myAnn = [[Annotation alloc]init]; 
    location.latitude = Sofia_LATITUDE; 
    location.longitude = Sofia_LONGITUDE; 
    myAnn.coordinate = location; 
    myAnn.title = @"Милото"; 
    myAnn.subtitle = @"Моята любов е ТУК!!!"; 
    [locations addObject:myAnn]; 

    myAnn = [[Annotation alloc]init]; 
    location.latitude = Plovdiv_LATITUDE; 
    location.longitude = Plovdiv_LONGITUDE; 
    myAnn.coordinate = location; 
    myAnn.title = @"Plovdiv"; 
    myAnn.subtitle = @"Plovdiv"; 
    [locations addObject:myAnn]; 

    myAnn = [[Annotation alloc]init]; 
    location.latitude = Varna_LATITUDE; 
    location.longitude = Varna_LONGITUDE; 
    myAnn.coordinate = location; 
    myAnn.title = @"Varna"; 
    myAnn.subtitle = @"Varna"; 
    [locations addObject:myAnn]; 


    [self.mapView addAnnotations:locations]; 


} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

Annotation.h

#import <Foundation/Foundation.h> 
#import <MapKit/MapKit.h> 


@interface Annotation : NSObject <MKAnnotation> 

@property (nonatomic, assign) CLLocationCoordinate2D coordinate; 
@property (nonatomic, copy) NSString * title; 
@property (nonatomic, copy) NSString * subtitle; 

@end 

Annotation.m

#import "Annotation.h" 

@implementation Annotation 

@synthesize coordinate, title, subtitle; 

@end 
Map.m

Map.h

#import <UIKit/UIKit.h> 
#import <MapKit/MapKit.h> 

@interface Map : UIViewController { 
    MKMapView * mapView; 
} 

@property(nonatomic,retain) IBOutlet MKMapView * mapView; 

- (IBAction)setMap:(id)sender; 

- (IBAction)getLocation:(id)sender; 


@end 

어디 있는지 모르겠어

그래서 프로그램을 실행하고 버튼을 클릭하여지도를 볼 때 표시되지만 중앙에 있지 않습니다. 내가 다시 움직이면서지도를보기 위해 클릭하면 모든 것이 정상입니다. 문제가 어디에 있습니까?

답변

1

지도보기의 영역을 설정해야합니다. 지역은 좌표 (가운데)와 스팬 (스팬의 숫자는지도에 표시된 세계의 각도를 나타냄)을 가지고 원하는 결과를 얻으려면 스팬 번호로 놀아 라. 좌표를 설정할 때 영역을 설정할 수 있습니다.

MKCoordinateRegion region; 
MKCoordinateSpan span; 
span.latitudeDelta = 0.2; 
span.longitudeDelta = 0.2; 
region.span = span; 
region.center = self.coordinate; 
[mapView setRegion:region animated:YES]; 
관련 문제