我尝试将我的核心数据实体作为注释添加到mapview及其工作中但我有两个问题1-它'只显示第一个注释图片和另一个注释有正常的图像(我想我必须在背景thrad做这个但我不知道我怎么能这样做)2-我需要得到选定的实体(注释)我怎么能这样做?
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
//add each object in Contacts entity to map view
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Contacts" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:nil];
for (info in fetchedObjects) {
NSLog(@"Name: %@", [info valueForKey:@"name"]);
//initializetion latitude and longitude
aLng=[[info valueForKey:@"lng"] doubleValue];
aLat=[[info valueForKey:@"lat"] doubleValue];
//if latitude and longitude not null
if(aLng && aLat && aLng!=0.0 &&aLat!=0.0)
{
//create a new Coordinate
CLLocationCoordinate2D wimLocation;
wimLocation.latitude=aLat;
wimLocation.longitude=aLng;
//create a new Annotation and initializetion it
MKPointAnnotation * myAnnotation=[MKPointAnnotation alloc];
myAnnotation.coordinate=wimLocation;
myAnnotation.title=[info valueForKey:@"name"];
//add create Annotation to mapview
[self.mapview addAnnotation:myAnnotation];
//default region Iran,Tehran
CLLocationCoordinate2D Location;
Location.latitude=35.696111;
Location.longitude=51.423056;
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=50.0; // change as per your zoom level
span.longitudeDelta=50.0;
region.span=span;
region.center= Location;
[self.mapview setRegion:region animated:TRUE];
[self.mapview regionThatFits:region];
}
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
// initializetion pinImage with contact image
UIImage *img = [UIImage imageWithData:[info valueForKey:@"photo"]];
if(img){
// If it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
// Handle any custom annotations.
if ([annotation isKindOfClass:[MKPointAnnotation class]])
{
// Try to dequeue an existing pin view first.
MKAnnotationView *pinView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
if (!pinView)
{
// If an existing pin view was not available, create one.
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];
//pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
//image size
CGSize destinationSize = CGSizeMake(35, 40);
UIGraphicsBeginImageContext(destinationSize);
[img drawInRect:CGRectMake(0,0,destinationSize.width,destinationSize.height)];
UIImage *pinImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
pinView.image = pinImage;
pinView.calloutOffset = CGPointMake(10, -20);
} else {
pinView.annotation = annotation;
}
return pinView;
}
}
return nil;
}
答案 0 :(得分:1)
显然,info
是一个局部变量,您在循环中一次又一次地覆盖。
相反,您应该获取正确的Contacts
托管对象来填充注释。在我看来,NSFetchedResultsController
是最好的解决方案。