iOS-MKMapView-Buggy注释

iOS-MKMapView-Buggy注释,ios,annotations,mkmapview,Ios,Annotations,Mkmapview,我有一个按钮,如果按下,我希望它得到用户的位置,并在上面放置一个注释。但好像我按下它一次,它添加注释在海洋中,然后我的用户位置出现(这工作)。我第二次按它,它会在我的位置上添加注释 代码: 听起来您是在CLLocationManager找到userlocation之前询问它的 通过设置mapView.showsUserLocation=YESmapview开始使用locationManager异步定位用户,因此可能无法立即找到正确的位置 您需要设置自定义CLLocationManager,并在按

我有一个按钮,如果按下,我希望它得到用户的位置,并在上面放置一个注释。但好像我按下它一次,它添加注释在海洋中,然后我的用户位置出现(这工作)。我第二次按它,它会在我的位置上添加注释

代码:


听起来您是在CLLocationManager找到userlocation之前询问它的

通过设置
mapView.showsUserLocation=YES
mapview开始使用
locationManager
异步定位用户,因此可能无法立即找到正确的位置

您需要设置自定义
CLLocationManager
,并在按钮上启动其位置更新,然后在找到位置后在其代理中设置注释

mapView.showsUserLocation = YES;

MKCoordinateRegion newRegion;

newRegion.center.latitude = mapView.userLocation.location.coordinate.latitude;
newRegion.center.longitude = mapView.userLocation.location.coordinate.longitude;

newRegion.span.latitudeDelta = 0.0004f;
newRegion.span.longitudeDelta = 0.0004f;

[mapView setRegion: newRegion animated: YES];


CLLocationCoordinate2D coordinate;
coordinate.latitude = mapView.userLocation.location.coordinate.latitude;
coordinate.longitude = mapView.userLocation.location.coordinate.longitude;

MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];

[annotation setCoordinate: coordinate];
[annotation setTitle: @"Your Car"];
[annotation setSubtitle: @"Come here for pepsi"];

[mapView addAnnotation: annotation];
[mapView setZoomEnabled: YES];
[mapView setScrollEnabled: YES];