2010-12-01 2 views
0

사용자의 현재 위치가 MKMapView에 표시되는 데 문제가 있습니다.MKMapView에서 사용자 위치 찾기 시도 중 문제가 발생했습니다.

헤더 : (부분)

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


@interface ParkingMapViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate> { 
    MKMapView *mapView; 
    CLLocationManager *locationManager; 
} 

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

-(void)loadAnnotations; 
-(void)showCurrentLocationButtonTapped:(id)sender; 

@end 

구현 다음은 관련 코드입니다

// ParkingMapViewController.m - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:100 target:self action:@selector(showCurrentLocationButtonTapped:)]; [self loadAnnotations]; CLLocationCoordinate2D centerCoord = { UCD_LATITUDE, UCD_LONGITUDE }; [mapView setCenterCoordinate:centerCoord zoomLevel:13 animated:NO]; //from "MKMapView+ZoomLevel.h" } - (void)showCurrentLocationButtonTapped:(id)sender { NSLog(@"Showing current location."); //[self.mapView setShowsUserLocation:YES]; //mapView.showsUserLocation = YES; //[sender setEnabled:NO]; //TODO: Uncomment and test locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m [locationManager startUpdatingLocation]; } - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { [mapView setCenterCoordinate:newLocation.coordinate]; if ([mapView showsUserLocation] == NO) { [mapView setShowsUserLocation:YES];//when this line is commented, there is no problem } [mapView setCenterCoordinate:newLocation.coordinate zoomLevel:13 animated:YES]; } - (void)viewDidUnload { [super viewDidUnload]; [mapView setShowsUserLocation:NO]; } - (void)dealloc { [locationManager release]; [mapView release]; [super dealloc]; } 

는 주석,지도보기에는 잘 응용 프로그램을 실행하고 모든 것이 있지만 현재 위치 버튼을 누르면지도가 새 위치로 이동하고 (약간 이동) 몇 초 후에 다시 충돌합니다. 내가 [mapView setShowsUserLocation:YES];을 주석 처리하면 아무 문제가 없지만, 그렇지 않은 경우는 콘솔에서이 오류를 뱉어 :

2010-11-30 22:57:20.657 Parking[53430:207] -[MKUserLocation annotationType]: unrecognized selector sent to instance 0x78427f0 
2010-11-30 22:57:20.659 Parking[53430:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MKUserLocation annotationType]: unrecognized selector sent to instance 0x78427f0' 
*** Call stack at first throw: 
(
    0 CoreFoundation      0x0266ab99 __exceptionPreprocess + 185 
    1 libobjc.A.dylib      0x027ba40e objc_exception_throw + 47 
    2 CoreFoundation      0x0266c6ab -[NSObject(NSObject) doesNotRecognizeSelector:] + 187 
    3 CoreFoundation      0x025dc2b6 ___forwarding___ + 966 
    4 CoreFoundation      0x025dbe72 _CF_forwarding_prep_0 + 50 
    5 Parking        0x00003ddb -[ParkingMapViewController mapView:viewForAnnotation:] + 64 
    6 MapKit        0x023a8130 -[MKAnnotationContainerView _addViewForAnnotation:] + 175 
    7 MapKit        0x023a2b2a -[MKAnnotationContainerView _addViewsForAnnotations:animated:] + 251 
    8 MapKit        0x0239e657 -[MKAnnotationContainerView showAddedAnnotationsAnimated:] + 137 
    9 MapKit        0x0237837c -[MKMapView _showAddedAnnotationsAndRouteAnimated:] + 102 
    10 MapKit        0x02376a88 -[MKMapViewInternal delayedShowAddedAnnotationsAnimated] + 191 
    11 Foundation       0x000571c9 __NSFireTimer + 125 
    12 CoreFoundation      0x0264bf73 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 19 
    13 CoreFoundation      0x0264d5b4 __CFRunLoopDoTimer + 1364 
    14 CoreFoundation      0x025a9dd9 __CFRunLoopRun + 1817 
    15 CoreFoundation      0x025a9350 CFRunLoopRunSpecific + 208 
    16 CoreFoundation      0x025a9271 CFRunLoopRunInMode + 97 
    17 GraphicsServices     0x02f4900c GSEventRunModal + 217 
    18 GraphicsServices     0x02f490d1 GSEventRun + 115 
    19 UIKit        0x002cfaf2 UIApplicationMain + 1160 
    20 Parking        0x000020e0 main + 102 
    21 Parking        0x00002071 start + 53 
) 
terminate called after throwing an instance of 'NSException' 

인터넷 검색이는 IB 문제가 가능성이 가장 높은 것으로 나타났다했지만 나는 그것을 발견 할 수 없었다. 어떤 도움든지 아주 많이 이해할 수있을 것이다

alt text

: 여기에 IB에서 설정 내의 화면입니다. 감사!

답변

2

viewForAnnotation 방법에 문제가있는 것 같습니다. 코드가 MKUserLocation annotation (해당 메소드가 없음)에서 annotationType을 호출하려고합니다.

viewForAnnotation에서 view이 어떤 종류의 주석을 요청했는지 확인하고 이에 따라 처리해야합니다.

그러나 MKMapView을 사용하는 경우 CLLocationManager을 사용하여 사용자의 current location을 가져올 필요가 없습니다. showsUserLocationYES으로 설정하면 map view에 사용자가있는 곳에 파란색 점이 표시됩니다.

두 경우 모두 viewForAnnotation 메서드는 annotation class 유형을 먼저 확인해야합니다. 이 같은 것이 상단에 있어야합니다.

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation 
{ 
    if ([annotation isKindOfClass:MKUserLocation.class]) { 
     //it's the built-in user location annotation, return nil to get default blue dot... 
     return nil; 
    } 

    //handle your custom annotations... 
} 
+0

그래, 고쳐주었습니다. 고마워요! – Stunner

+0

마찬가지로 호기심 :이 방법으로 검색된 사용자 위치의 정확성은 무엇입니까? 어떻게 든 최고야? –

+0

@ NicolaeSurdu : [이 답변보기] (http://stackoverflow.com/questions/6234978/iphone-app-questions-on-interaction-between-cllocationmanager-and-mkmapview-sho/6294388#6294388). – Anna

관련 문제