在当前位置放置Pin

我正在构build一个iOS应用程序,需要能够将一个别针放在用户当前所在的位置! 出于某种原因,我已经有一段可怕的时间了! 我尝试了下面的代码,但遇到了一个错误。

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{ if (annotation == mapView.userLocation) { MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil]; annView.pinColor = MKPinAnnotationColorRed; annView.animatesDrop=TRUE; annView.canShowCallout = YES; annView.calloutOffset = CGPointMake(-5, 5); return annView; [annView release]; } } 

错误是:控制可能达到非void函数的结束。

十分感谢你的帮助! “欣赏吧!

以下代码在Xcode iPhone模拟器中正常工作:

 #import "ViewController.h" @import MapKit; @interface ViewController () <MKMapViewDelegate> @property (weak, nonatomic) IBOutlet MKMapView *mapView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self.mapView setDelegate:self]; [self.mapView setShowsUserLocation:YES]; } - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { // zoom to region containing the user location MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800); [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES]; // add the annotation MKPointAnnotation *point = [[MKPointAnnotation alloc] init]; point.coordinate = userLocation.coordinate; point.title = @"The Location"; point.subtitle = @"Sub-title"; [self.mapView addAnnotation:point]; } @end 

当一个方法有一个返回types,那么在所有条件下必须返回一些东西,你只能在if分支中返回一些东西,否则不会。 编译器要求你在if语句的计算结果为false时也返回一些东西,这就是你得到编译错误的原因。

顺便说一下这条线

  [annView release]; 

永远不能执行,因为你在那之前回来。 但是你有什么理由为此调用? 即试图使用非ARC代码? 你从某处(这是旧的代码)复制这行吗?