由于某种原因,每当我点击我的pin时,应用程序就会崩溃。我的代码中没有错误或警告。只有当我试图获得从userLocation到pin的距离时,才会发生这种情况。发现我的错误了吗?
@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(苹果版本gdb-1518)(2011年2月12日星期六02:52:12 UTC)版权所有2004自由软件基金会,股份有限公司。GDB是自由软件,受GNU通用公共许可证保护,您欢迎在特定条件下更改和/或分发副本。键入"显示复制"以查看条件。GDB绝对没有保修。键入"显示保修"以了解详细信息。此GDB配置为"x86_64-apple-darwin"。附加到进程23386。2011-06-02 00:39:46.146酸奶店定位器[2386:207]32.606434-85.4840252011-06-02 00:39:46.148酸奶店定位器[2386:207]33.479775-86.7909772011-06-02 00:39:53.121 YogurtStoreLocator[2386:207]-[NSCFNumber stringByStandardingWhitespace]:无法识别的选择器发送到实例0x587b2d02011-06-02 00:39:53.123 YogurtStoreLocator[23386:207]*由于未捕获异常"NSInvalidArgumentException"而终止应用程序,原因:"-[NSCFNumber stringByStandardingWhitespace]:无法识别的选择器发送到实例0x587b2d0"*第一次投掷时调用堆栈:(0 CoreFoundation 0x00fca5a9异常预处理+1851 libobjc。A.dylib 0x0111e313对象异常抛出+442 CoreFoundation 0x00fcc0bb-[NSObject(NSObject)doesNotRecognizeSelector:]+1873 CoreFoundation 0x00f3b966 __转发+9664核心基础0x00f3b522 _CF_forwarding_prep_0+505 UIKit 0x0034c340-[UICalloutView集字幕:动画:]+886 UIKit 0x0034c010-[UICalloutView集字幕:]+497 MapKit 0x000bb0ac-[MKAnnotationContainerView _showBubbleForAnnotationView:反弹:滚动到适合:userInitiated:避免:]+5378 MapKit 0x000bc3ed-[MKAnnotationContainerView _setSelectedAnnotationView:反弹:按下:滚动到适合:userInitiated:避免:]+6539 MapKit 0x000b54bc-[MKAnnotationContainerView_setSelectedAnnotationView:bound:pressed:scrollToFit:userInitiated:]+14410 MapKit 0x0008cab3-[MKMapView手柄地图:]+45911 UIKit 0x004ad4f2-[UIGatureRecognizer _updateGestureWithEvent:]+73012 UIKit 0x004a94fe-[UIGatureRecognizer _delayerdUpdateGesture]+4713 UIKit 0x004fafc _UIGestureRecognizerUpdateObserver+58414核心基础0x00fab89bCFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION+2715核心基础0x00f406e7 __CFRunLoopDoObserver+29516核心基础0x00f091d7_CFRunLoopRun+157517 CoreFoundation 0x00f08840 CFRunLoopRunSpecific+20818 CoreFoundation 0x00f08761 CFRunLoopRunInMode+9719 GraphicsServices 0x01f071c4 GSEventRunModal+21720 GraphicsServices 0x01f07289 GSEventRun+11521 UIKit 0x0022ac93 UIApplicationMain+116022酸奶店定位器0x00002099 main+12123 YogurtStoreLocator 0x00002015开始+53)在引发"NSException"的实例后调用了terminatesharedlbrary应用加载规则all当前语言:auto;当前目标-c(gdb)
仅将showsUserLocation
设置为YES
是不够的。地图视图需要获取这些数据。这是一个异步过程,数据只会在一段时间后更新。通过mapView:didUpdateUseLocation:
方法将此更新通知给代理。您应该实现该方法,并且只有在获得用户位置后才调用showPin
。