0

何らかの理由で、ピンをクリックするたびにアプリがクラッシュします。私のコードにはエラーや警告はありません。userLocation からピンまでの距離を取得しようとしたときにのみ発生します。私の間違いを見つけますか?

@implementation FirstViewController
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{   
mapView.showsUserLocation = YES;

[super viewDidLoad];
}
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation   { 

[self showPin];
[self updateLocation];
}
-(IBAction) updateLocation{
mapView.showsUserLocation = YES;
}
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}  


- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc. that aren't in use.
}


- (void)viewDidUnload
{
[super viewDidUnload];

// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


-(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;
}

}}
- (void)dealloc
{
[super dealloc];
}
- (MKAnnotationView *)mapView:(MKMapView *)mv viewForAnnotation:(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pinView"];
if (!pinView) {
    pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pinView"] autorelease];
    pinView.pinColor = MKPinAnnotationColorRed;
    pinView.animatesDrop = YES;
    pinView.canShowCallout = YES;


    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    pinView.rightCalloutAccessoryView = rightButton;
} else {
    pinView.annotation = annotation;
}
if (annotation == mapView.userLocation){
    return nil; //default to blue dot
}
return pinView;
}
-(IBAction) showPin{
//MOST CODE WILL BE INSERTED HERE!
//instructions

CLLocation *usersLocation = [[CLLocation alloc]initWithLatitude:mapView.userLocation.coordinate.latitude longitude:mapView.userLocation.coordinate.longitude];
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2;
//ignore above
//always declare location as companyCity 
//if more than one in one city then add a number i.e. companyCity2
//chili in mobile alabama
CLLocationCoordinate2D chiliAuburn = mapView.userLocation.coordinate;

chiliAuburn.latitude = 32.606434 ;
chiliAuburn.longitude = -85.484025;
region.span=span;
region.center=chiliAuburn;
CLLocation *chiliAuburnLocation = [[CLLocation alloc] initWithLatitude:chiliAuburn.latitude longitude:chiliAuburn.longitude];
if(chiliAuburnAlabama != nil) {
    [mapView removeAnnotation:chiliAuburnAlabama];
    [chiliAuburnAlabama release];
    chiliAuburnAlabama = nil;
}

chiliAuburnAlabama = [[AddressAnnotation alloc] initWithCoordinate:chiliAuburn];
[chiliAuburnAlabama setTitle:@"Chili Yogurt Café"];
double distanceMiles = ([usersLocation distanceFromLocation:chiliAuburnLocation]/1609.334);
[chiliAuburnAlabama setSubTitle:[NSString stringWithFormat:@"%.2fm", distanceMiles]];
//[chiliAuburnAlabama setSubTitle:@"TEST"];
[mapView addAnnotation:chiliAuburnAlabama];
[chiliAuburnAlabama release];
//tutti frutti homewood alabama
CLLocationCoordinate2D tuttifruttiHomewood = mapView.userLocation.coordinate;

tuttifruttiHomewood.latitude = 33.479775 ;
tuttifruttiHomewood.longitude = -86.790977;
region.span=span;
region.center=tuttifruttiHomewood;

if(tuttifruttiHomewoodAlabama != nil) {
    [mapView removeAnnotation:tuttifruttiHomewoodAlabama];
    [tuttifruttiHomewoodAlabama release];
    tuttifruttiHomewoodAlabama = nil;
}

tuttifruttiHomewoodAlabama = [[AddressAnnotation alloc] initWithCoordinate:tuttifruttiHomewood];
[tuttifruttiHomewoodAlabama setTitle:@"Tutti Frutti"];
[tuttifruttiHomewoodAlabama setSubTitle:@"YOUR RATING HERE"];
[mapView addAnnotation:tuttifruttiHomewoodAlabama];


//ignore below
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];

}
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views {
for(MKAnnotationView *annotationView in views) {
    if(annotationView.annotation == mv.userLocation) {
        MKCoordinateRegion region;
        MKCoordinateSpan span;

        span.latitudeDelta=0.1;
        span.longitudeDelta=0.1; 

        CLLocationCoordinate2D location=mv.userLocation.coordinate;

        region.span=span;
        region.center=location;

        [mv setRegion:region animated:TRUE];
        [mv regionThatFits:region];
    }

}
}



@end

GNU gdb 6.3.50-20050815 (Apple バージョン gdb-1518) (Sat Feb 12 02:52:12 UTC 2011) Copyright 2004 Free Software Foundation, Inc. GDB はフリー ソフトウェアであり、GNU General Public License の対象です。特定の条件下での変更および/またはコピーの配布を歓迎します。条件を表示するには、「showcopy」と入力します。GDB の保証は一切ありません。詳細については、「保証を表示」と入力してください。この GDB は「x86_64-apple-darwin」として構成されました。プロセス 23386 にアタッチしています。 23386:207] 33.479775、-86.790977 2011-06-02 00:39:53.121 YogurtStoreLocator[23386:207] -[NSCFNumber stringByStandardizingWhitespace]: 認識されないセレクターがインスタンス 0x587b2d0 に送信されました 2011-06-02[00:39:53.12YogurtStore3: YogurtStore3* キャッチされない例外 'NSInvalidArgumentException' によるアプリの終了、理由: '-[NSCFNumber stringByStandardizingWhitespace]: 認識されないセレクターがインスタンス 0x587b2d0 に送信されました' *最初のスロー時のコール スタック: ( 0 CoreFoundation 0x00fca5a9 exceptionPreprocess + 185 1 libobjc.A.dylib 0x0111e313 objc_exception_throw + 44 2 CoreFoundation 0x00fcc0bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187 3 CoreFoundation 0x00f3b966 __転送+ 966 4 CoreFoundation 0x00f3b522 _CF_forwarding_prep_0 + 50 5 UIKit 0x0034c340 -[UICalloutView setSubtitle:animated:] + 88 6 UIKit 0x0034c010 -[UICalloutView setSubtitle:] + 49 7 MapKit 0x000bb0ac -[MKAnnotationContainerView _showBubbleForAnnotationView:bounce:scrollToFit:userInitiated:avoid:] + 537 8 MapKit 0x000bc3ed -[MKAnnotationContainerView _setSelectedAnnotationView:bounce:pressed:scrollToFit:userInitiated:avoid:] + 653 9 MapKit 0x000b54bc -[MKAnnotationContainerView _setSelectedAnnotationView:bounce:pressed:scrollToFit:userInitiated:] + 144 144 MapViewca:handleMap0x30 マップ] + 459 11 UIKit 0x004ad4f2 -[UIGestureRecognizer _updateGestureWithEvent:] + 730 12 UIKit 0x004a94fe -[UIGestureRecognizer _delayedUpdateGesture] + 47 13 UIKit 0x004afafc _UIGestureRecognizerUpdateObserver + 584 14 CoreFoundation 89b0fab0b0b0bserverCFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION+ 27 15 CoreFoundation 0x00f406e7 __CFRunLoopDoObservers + 295 16 CoreFoundation 0x00f091d7 __CFRunLoopRun + 1575 17 CoreFoundation 0x00f08840 CFRunLoopRunSpecific + 208 18 CoreFoundation 0x00f08761 CFRunLoopRunInMode + 97 19 GraphicsServices 0x01f071c4 GSEventRunModal + 217 20 GraphicsServices 0x01f07289 GSEventRun + 115 21 UIKit 0x0022ac93 UIApplicationMain + 1160 22 YogurtStoreLocator 0x00002099 main + 121 23 YogurtStoreLocator 0x00002015 start + 53 ) 「NSException」のインスタンスをスローした後に呼び出されて終了します。 sharedlibrary apply-load-rules all 現在の言語: auto; 現在は目的の c (gdb)

4

1 に答える 1

2

に設定showsUserLocationするだけYESでは不十分です。マップ ビューはそのデータをフェッチする必要があります。これは非同期プロセスであり、データはしばらくしてから更新されます。mapView:didUpdateUseLocation:デリゲートには、メソッドを介してこの更新が通知されます。そのメソッドを実装しshowPin、ユーザーの場所を取得した後にのみ呼び出しを行う必要があります。

于 2011-06-02T04:21:32.073 に答える