Ios 在背景中将注释添加到地图
我尝试将我的核心数据实体作为注释添加到mapview中,这是可行的,但我有两个问题1-它只显示第一个注释图片,而另一个注释具有正常的pin图像(我想我必须在背景thrad中执行此操作,但我不知道如何执行此操作)2-我需要获取选定的实体(注释)如何执行此操作Ios 在背景中将注释添加到地图,ios,objective-c,core-data,mkmapview,Ios,Objective C,Core Data,Mkmapview,我尝试将我的核心数据实体作为注释添加到mapview中,这是可行的,但我有两个问题1-它只显示第一个注释图片,而另一个注释具有正常的pin图像(我想我必须在背景thrad中执行此操作,但我不知道如何执行此操作)2-我需要获取选定的实体(注释)如何执行此操作 -(void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; //add each object in Contacts entity to map
-(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;
}
-(void)视图显示:(BOOL)动画{
[超级视图显示:动画];
//将联系人实体中的每个对象添加到地图视图
NSManagedObjectContext*上下文=[self-managedObjectContext];
NSFetchRequest*fetchRequest=[[NSFetchRequest alloc]init];
NSEntityDescription*实体=[NSEntityDescription]
entityForName:@“Contacts”在ManagedObjectContext:context]中;
[FetchRequestSetEntity:entity];
NSArray*fetchedObjects=[context executeFetchRequest:fetchRequest错误:nil];
for(获取对象中的信息){
NSLog(@“Name:%@,[info valueForKey:@“Name]”);
//初始化经纬度
aLng=[[info valueForKey:@“lng”]doubleValue];
aLat=[[info valueForKey:@“lat”]doubleValue];
//如果纬度和经度不为空
如果(aLng&&aLat&&aLng!=0.0&&aLat!=0.0)
{
//创建新坐标
CLLOCATIONCoordinated2D WIM定位;
纬度=aLat;
wimLocation.longitude=aLng;
//创建新注释并对其进行初始化
MKPointAnnotation*myAnnotation=[MKPointAnnotation alloc];
myAnnotation.coordinate=wimLocation;
myAnnotation.title=[info valueForKey:@“name”];
//将创建注释添加到mapview
[self.mapview addAnnotation:myAnnotation];
//默认区域伊朗、德黑兰
CLLOCATION坐标2D定位;
位置:纬度=35.696111;
位置.经度=51.423056;
协调区域;
Mk坐标跨度;
span.latitudeDelta=50.0;//根据缩放级别进行更改
span.纵向德尔塔=50.0;
地区span=span;
区域。中心=位置;
[self.mapview setRegion:区域动画:TRUE];
[self.mapview regionatfits:region];
}
}
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释
{
//初始化pinImage与contact image
UIImage*img=[UIImage imageWithData:[info valueForKey:@“photo”];
如果(img){
//如果是用户位置,只需返回nil。
if([annotation isKindOfClass:[MKUserLocation类]])
返回零;
//处理任何自定义注释。
if([annotation isKindOfClass:[MKPointAnnotation class]])
{
//首先尝试将现有pin视图出列。
MKAnnotationView*pinView=(MKAnnotationView*)[mapView出列可重用AnnotationView,标识符:@“CustomPinAnnotationView”];
如果(!pinView)
{
//如果现有接点视图不可用,请创建一个。
pinView=[[MKAnnotationView alloc]initWithAnnotation:annotation重用标识符:@“CustomPinAnnotationView”];
//pinView.animatesDrop=是;
pinView.canShowCallout=是;
//图像大小
cgsizedestinationsize=CGSizeMake(35,40);
UIGraphicsBeginImageContext(destinationSize);
[img drawInRect:CGRectMake(0,0,destinationSize.width,destinationSize.height)];
UIImage*pinImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
pinView.image=pinImage;
pinView.calloutOffset=CGPointMake(10,-20);
}否则{
pinView.annotation=注释;
}
返回pinView;
}
}
返回零;
}
显然,info
是一个局部变量,您在循环中一次又一次地覆盖它
相反,您应该获得正确的
联系人
托管对象来填充注释。在我看来,NSFetchedResultsController
将是最好的解决方案 您可以声明dispatch\u queue\u t addPinQueue;dispatch_async(addPinQueue,^{调用您的方法获取MapPin的所有信息(标题、副标题、经度、纬度)});dispatch_async(dispatch_get_main_queue(),^{调用实际添加Pin的方法}):您能给我一个示例吗?当然。只需创建一个新项目,例如“主细节”和检查“核心数据”。在主视图控制器中检查获取的结果控制器。请根据我的代码填写答案,谢谢。