我尝试将我的核心数据实体作为注释添加到 mapview 并且它有效但我有两个问题 1-它只显示第一张注释图片而另一个注释具有正常的图钉图像(我想我必须在后台执行此操作但是我不知道我该怎么做)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;
}
最佳答案
显然,info
是您在循环中一次又一次覆盖的局部变量。
相反,您应该获得正确的 Contacts
托管对象来填充您的注释。 NSFetchedResultsController
在我看来是最好的解决方案。
关于ios - 在背景中为 map 添加注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27366491/