【发布时间】:2014-12-12 06:07:44
【问题描述】:
我在这里遇到了奇怪的问题。我不知道我要对这个注释做什么。我得到多个注释。它应该是显示我位置的一个注释。这是屏幕
这是我的代码
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
static NSString *identifier = @"MyLocation";
MKAnnotationView *annotationView = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.image = [UIImage imageNamed:@"arrest.png"]; // since you're setting the image, you could use MKAnnotationView instead of MKPinAnnotationView
} else {
NSLog(@"hello world");
annotationView.annotation = annotation;
}
return annotationView;
}
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 350, 350);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
MKPointAnnotation * point= [[MKPointAnnotation alloc] init];
point.coordinate = userLocation.coordinate;
point.title = @"Where am I?";
point.subtitle = @"I'm here!!!";
[self.mapView addAnnotation:point];
}
-(void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views{
MKAnnotationView *annotationView = [views objectAtIndex:0];
id<MKAnnotation> mp = [annotationView annotation];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate] ,350,350);
[UIView animateWithDuration:5.0 animations:^{
[self.mapView setRegion:region animated:YES];
} completion:^(BOOL finished) {
[self.mapView selectAnnotation:mp animated:YES];
}];
}
我对这个项目只有简单的场景。获取位置,更改标题,然后查看当前地址。但是如果我使用这个-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation,它可以正常工作,但我不能更改注释的标题。为什么我得到多个注释?希望得到您的帮助。这是我第一次在 iOS 中使用这个地图工具包。
【问题讨论】:
标签: ios annotations mkannotation