【发布时间】:2016-03-09 11:35:23
【问题描述】:
我一直在寻找几个小时来找到一个关于如何在给定包含对象位置和图像的类(在我的例子中,对象称为 EVENT)的情况下添加注释的好教程。
看看我到目前为止做了什么,我从 viewDidLoad 调用这个方法 setupAnnotations。
- (void)setupAnnotations {
_allEvents = [EventEntity MR_findAll];
for (EventEntity *event in _allEvents) {
AVEventAnnotationView *annotation = [[AVEventAnnotationView alloc] initWithOperator:event];
CLLocation *eventLocation = [[CLLocation alloc] initWithLatitude:[[event latitude] doubleValue] longitude:[[event longitude] doubleValue]];
annotation.coordinate = eventLocation.coordinate;
MKPlacemark *eventPlacemark = [[MKPlacemark alloc] initWithCoordinate:eventLocation.coordinate addressDictionary:nil];
MKMapItem *eventItem = [[MKMapItem alloc] initWithPlacemark:eventPlacemark];
// Im loading image from URL for or based on event object
UIImageView *imageContainer = [[UIImageView alloc] init];
[imageContainer sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",fullBannerImgPath,[event bannerImg]]]];
[_bannerImagesArray addObject:imageContainer];
BOOL hasAnnotation = NO;
for (AVEventAnnotationView *ann in self.mapView.annotations) {
if([ann isKindOfClass:[AVEventAnnotationView class]])
{
if([[annotation.eventEntity dataId] isEqualToString:[ann.eventEntity dataId]])
{
hasAnnotation = YES;
break;
}
}
}
if(!hasAnnotation)
[self.mapView addAnnotation:annotation];
}
}
这是我的 mapview 委托:
- (MKAnnotationView *)mapView:(MKMapView *)mapViewIn viewForAnnotation:(id <MKAnnotation>)annotation {
if (mapViewIn != self.mapView || [annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
static NSString *annotationIdentifier = @"SPGooglePlacesAutocompleteAnnotation";
MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
// STUCK HERE
annotationView.image = [UIImage imageNamed:@"LocationBlue"];
annotationView.frame = CGRectMake(0, 0, 80, 80);
annotationView.draggable = NO;
annotationView.canShowCallout = YES;
return annotationView;
}
我是实施 MKMapView 的新手。前段时间刚看了注解和注解视图的区别。
无论如何,回到我的主要问题或我的主要问题:如果我有一个包含纬度、经度和图像的事件数组,如何将注释/注释视图放入 mkmapview?
【问题讨论】:
标签: ios mkmapview mkannotationview