如何动态放置地图注释?

问题描述:

我创建具有多个注释的地图,但我不知道如何动态(我有他们在CoreData中)。如何动态放置地图注释?

我puting他们固定的验证码

- (void)viewDidLoad { 
    [super viewDidLoad]; 


    CLLocation *userLoc = mapView.userLocation.location; 
    CLLocationCoordinate2D userCoordinate = userLoc.coordinate; 

    NSLog(@"user latitude = %f",userCoordinate.latitude); 
    NSLog(@"user longitude = %f",userCoordinate.longitude); 

    mapView.delegate=self; 

    NSMutableArray* annotations=[[NSMutableArray alloc] init]; 




    CLLocationCoordinate2D theCoordinate1; 
    theCoordinate1.latitude = 43.82078; 
    theCoordinate1.longitude = 15.307265; 

    CLLocationCoordinate2D theCoordinate2; 
    theCoordinate2.latitude = 44.91035917; 
    theCoordinate2.longitude = 14.65576172; 



    MyAnnotation* myAnnotation1=[[MyAnnotation alloc] init]; 

    myAnnotation1.coordinate=theCoordinate1; 
    [email protected]"Rohan"; 
    [email protected]"in the city"; 

    MyAnnotation* myAnnotation2=[[MyAnnotation alloc] init]; 

    myAnnotation2.coordinate=theCoordinate2; 
    [email protected]"Vaibhav"; 
    [email protected]"on a Bridge"; 



    [mapView addAnnotation:myAnnotation1]; 
    [mapView addAnnotation:myAnnotation2]; 


    [annotations addObject:myAnnotation1]; 
    [annotations addObject:myAnnotation2]; 


    NSLog(@"%d",[annotations count]); 
    //[self gotoLocation];//to catch perticular area on screen 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 

    // Walk the list of overlays and annotations and create a MKMapRect that 
    // bounds all of them and store it into flyTo. 
    MKMapRect flyTo = MKMapRectNull; 
    for (id <MKAnnotation> annotation in annotations) { 
     NSLog(@"fly to on"); 
     MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate); 
     MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0); 
     if (MKMapRectIsNull(flyTo)) { 
      flyTo = pointRect; 
     } else { 
      flyTo = MKMapRectUnion(flyTo, pointRect); 
      //NSLog(@"else-%@",annotationPoint.x); 
     } 
    } 


    // Position the map so that all overlays and annotations are visible on screen. 
    mapView.visibleMapRect = flyTo; 


    UIBarButtonItem* temp=[[UIBarButtonItem alloc] init]; 
    [email protected]"Back"; 
    self.navigationItem.backBarButtonItem=temp; 
    //[temp release]; 
} 

附:如果您需要额外的代码,请告诉。

感谢对帮助

UPDATE:

Locations.h

... 
@interface Locations : NSManagedObject 

@property (nonatomic, retain) NSDecimalNumber * lat; 
@property (nonatomic, retain) NSDecimalNumber * lon; 
... 

更新2:

这里就是我puting注释示例代码“固定”。

http://dl.dropbox.com/u/77033905/stackoverflow/Map.zip

我怎样才能在阵列添加此注解?定义这个位置(坐标,标题,subitle阵列,并将它们添加到地图。)

+0

你的动态是什么意思? –

+0

在PHP中,我把它们放在这里:$ array [] = $ annotation;等等。 – CroiOS

+0

还是不知道。让我知道你到底想做什么。 –

位置实例将是你的注释只是做

[mapView addAnnotations:locationArray]; 

Location.h

... 
@interface Locations : NSManagedObject <MKAnnotation>{ 
    CLLocationCoordinate2D coordinate; 
} 

@property (nonatomic, retain) NSDecimalNumber * lat; 
@property (nonatomic, retain) NSDecimalNumber * lon; 
... 

Location.m

... 
- (CLLocationCoordinate2D)coordinate 
{ 
coordinate.latitude = [self.lat doubleValue]; 
coordinate.longitude = [self.lon doubleValue]; 
return coordinate; 
} 

- (NSString *)title 
{ 
return @"yourTitle"; 
} 

- (NSString *)subtitle 
{ 
return @"yourSubtitle"; 
} 

这是你想要的吗?

+0

对不起,但没有。我想在第一篇文章中的代码中添加注释,但在数组中添加注释。我更新了我的第一篇文章。 – CroiOS

+0

我必须睡觉。现在是凌晨4点。 –

+0

这里是22:00 :) – CroiOS