2010-12-01 5 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に設定私の画面です。ありがとう!それはviewForAnnotation方法であるように

答えて

2

問題が見えます。コードは、(このような方法を持っていません)MKUserLocation annotationannotationTypeを呼び出そうとしています。 viewForAnnotation

、それはviewがために要求されている注釈の種類をチェックし、それに応じて処理する必要があります。

ただし、MKMapViewを使用している場合は、CLLocationManagerを使用してユーザーのcurrent locationを取得する必要はありません。ただ、YESからshowsUserLocationを設定すること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

関連する問題