尝试获取距用户位置的距离以固定在地图上时 mkmap 崩溃

发布于 2024-11-13 19:38:23 字数 7213 浏览 2 评论 0原文

由于某种原因,每当我点击我的图钉时,应用程序就会崩溃。我的代码中没有错误或警告。仅当我尝试获取从 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(Apple 版本 gdb-1518)(UTC 2011 年 2 月 12 日星期六 02:52:12) 版权所有 2004 自由软件基金会,Inc. GDB 是免费软件,受 GNU 通用公共许可证保护,您可以 欢迎在某些条件下对其进行更改和/或分发其副本。 输入“显示复制”以查看条件。 GDB 绝对没有任何保证。输入“显示保修”以了解详细信息。 该 GDB 被配置为“x86_64-apple-darwin”。附加到进程 23386。 2011-06-02 00:39:46.146 YogurtStoreLocator[23386:207] 32.606434,-85.484025 2011-06-02 00:39:46.148 YogurtStoreLocator[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.123 YogurtStoreLocator[23386:207] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[NSCFNumber stringByStandardizingWhitespace]:无法识别的选择器发送到实例 0x587b2d0” *第一次抛出时的调用堆栈: ( 0 CoreFoundation 0x00fca5a9 异常预处理 + 185 1 libobjc.A.dylib 0x0111e313 objc_异常_抛出 + 44 2 CoreFoundation 0x00fcc0bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187 3 CoreFoundation 0x00f3b966 __转发 + 966 4 核心基础 0x00f3b522 _CF_forwarding_prep_0 + 50 5 UIKit 0x0034c340 -[UICalloutView setSubtitle:动画:] + 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 10 MapKit 0x0008cab3 -[MKMapView 句柄Tap:] + 459 11 UIKit 0x004ad4f2-[UIGestureRecognizer_updateGestureWithEvent:] + 730 12 UIKit 0x004a94fe -[UIGestureRecognizer _delayedUpdateGesture] + 47 13 UIKit 0x004afafc_UIGestureRecognizerUpdateObserver + 584 14 CoreFoundation 0x00fab89b CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 27 15 核心基础 0x00f406e7 __CFRunLoopDoObservers + 295 16 核心基础 0x00f091d7 __CFRunLoopRun + 1575 17 核心基础 0x00f08840 CFRunLoopRunSpecific + 208 18 核心基础 0x00f08761 CFRunLoopRunInMode + 97 19 图形服务 0x01f071c4 GSEventRunModal + 217 20 图形服务 0x01f07289 GSEventRun + 115 21 UIKit 0x0022ac93 UIApplicationMain + 1160 22 酸奶店定位器 0x00002099 主 + 121 23 酸奶商店定位器 0x00002015 开始 + 53 ) 抛出“NSException”实例后调用终止 共享库应用加载规则全部 当前语言:自动;目前objective-c (gdb)

For some reason whenever i click on my pin the app crashes. there are no errors or warnings in my code. It only happens when i try to get the distance from userLocation to the pin. find my mistake?

@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 version gdb-1518) (Sat Feb 12 02:52:12 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".Attaching to process 23386.
2011-06-02 00:39:46.146 YogurtStoreLocator[23386:207] 32.606434,-85.484025
2011-06-02 00:39:46.148 YogurtStoreLocator[23386:207] 33.479775,-86.790977
2011-06-02 00:39:53.121 YogurtStoreLocator[23386:207] -[NSCFNumber stringByStandardizingWhitespace]: unrecognized selector sent to instance 0x587b2d0
2011-06-02 00:39:53.123 YogurtStoreLocator[23386:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFNumber stringByStandardizingWhitespace]: unrecognized selector sent to instance 0x587b2d0'
*
Call stack at first throw:
(
0 CoreFoundation 0x00fca5a9 exceptionPreprocess + 185
1 libobjc.A.dylib 0x0111e313 objc_exception_throw + 44
2 CoreFoundation 0x00fcc0bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00f3b966 __forwarding
+ 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
10 MapKit 0x0008cab3 -[MKMapView handleTap:] + 459
11 UIKit 0x004ad4f2 -[UIGestureRecognizer _updateGestureWithEvent:] + 730
12 UIKit 0x004a94fe -[UIGestureRecognizer _delayedUpdateGesture] + 47
13 UIKit 0x004afafc _UIGestureRecognizerUpdateObserver + 584
14 CoreFoundation 0x00fab89b CFRUNLOOP_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
)
terminate called after throwing an instance of 'NSException'
sharedlibrary apply-load-rules all
Current language: auto; currently objective-c
(gdb)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花细雨 2024-11-20 19:38:23

仅将 showsUserLocation 设置为 YES 是不够的。地图视图需要获取该数据。这是一个异步过程,数据只会在一段时间后更新。通过 mapView:didUpdateUseLocation: 方法向委托通知此更新。您应该实现该方法,并仅在获得用户位置后才调用 showPin

Just setting showsUserLocation to YES isn't sufficient. The map view needs to fetch that data. This is an asynchronous process and the data will be updated only after a while. The delegate is notified of this update via mapView:didUpdateUseLocation: method. You should implement that method and make a call to showPin only after you've the user location.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文