2015-01-16 3 views
1

내 앱의지도보기에 문제가 있습니다. 나는 클릭 할 때지도에서 사용자 위치를 표시해야하지만 오류 메시지가 나타나지 않는 아무 것도 발생하지 않는 버튼을 만들었습니다.버튼에 연결된 사용자 위치 작업이 작동하지 않습니다.

나는이 문제가 내가 위임자를 작성한 방식으로 거짓말을할지 모른다. 관련 .H와하는 .m 파일의 코드는 다음과 같습니다 :

mapViewController.h

#import <UIKit/UIKit.h> 
#import <MapKit/MapKit.h> 
#import <CoreLocation/CoreLocation.h> 
@interface mapViewController : UIViewController { 
MKMapView *mapview; 
} 
@property (nonatomic, retain) IBOutlet MKMapView *mapview; 
-(IBAction)setMap:(id)sender; 
-(IBAction)getCurrentLocation:(id)sender; 
@property (nonatomic, retain) IBOutlet CLLocationManager *locationManager; 
@end 

에게 어떤 도움을 크게 감상 할 수 mapViewController.m

#import "mapViewController.h" 
@interface mapViewController() 
@end 
@implementation mapViewController { 
CLLocationManager *locationManager; 
} 
@synthesize mapview; 
- (void)viewDidLoad { 
[super viewDidLoad]; 
// Do any additional setup after loading the view. 
self.locationManager.delegate=self; 
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
self.locationManager.requestWhenInUseAuthorization; 
} 
- (void)didReceiveMemoryWarning { 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 
-(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; 
} 
} 
-(IBAction)getCurrentLocation:(id)sender { 
mapview.showsUserLocation = YES; 
} 
@end 

, 감사

+0

예상되는 결과는 무엇입니까? 액션은 Interface Builder의 버튼에 어떻게 연결되어 있습니까? – quellish

+0

위에서 언급했듯이 버튼을 클릭하면 사용자의 위치가지도에 표시됩니다. getCurrentLocation을 참조하는 버튼에 연결된 이벤트 (내부 터치)가 있습니다 – KGNZ

+0

문서에서 사용자의 위치가 실제로지도에 표시되는지 여부는 표시되지 않습니다. 맵보기가 표시하려고 시도해야합니다. " . 이 속성을 YES로 설정하는 것 외에도 찾고있는 추측 동작을 얻기 위해 중심 좌표 및 (가능할 경우) 확대/축소를 설정해야합니다. – quellish

답변

0

MapKit 대리자를 구현해야합니다. 우리는을 관찰 할 2 이벤트 관찰자를 추가

: 앱 전경 여부에 처리하기 위해 :

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { 
    MKCoordinateRegion region; 
    MKCoordinateSpan span; 
    span.latitudeDelta = 0.005; 
    span.longitudeDelta = 0.005; 
    CLLocationCoordinate2D location; 
    location.latitude = userLocation.coordinate.latitude; 
    location.longitude = userLocation.coordinate.longitude; 
    region.span = span; 
    region.center = location; 
    [mapView setRegion:region animated:YES]; 
} 

추가가 (당신이 당신의 뷰 컨트롤러의 .h에 위임 서명을 추가해야합니다) 앱이 백그라운드로 들어가거나 활성 상태로 돌아 오는 중 :

- (void)viewDidLoad{ 
    [super viewDidLoad]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appToBackground) name:UIApplicationDidEnterBackgroundNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appReturnsActive) name:UIApplicationDidBecomeActiveNotification object:nil]; 
} 

- (void)appToBackground{ 
    [mapview setShowsUserLocation:NO]; 
} 

- (void)appReturnsActive{ 
    [mapview setShowsUserLocation:YES]; 
} 
관련 문제