2012-05-23 60 views
2

我創建具有多個註釋的地圖,但我不知道如何動態(我有他們在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

還是不知道。讓我知道你到底想做什麼。 –

回答

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