2

我正在创建带有多个注释的地图,但我不知道如何动态(我在 CoreData 中有它们)。

我用这段代码修复它们:

- (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;
    myAnnotation1.title=@"Rohan";
    myAnnotation1.subtitle=@"in the city";

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

    myAnnotation2.coordinate=theCoordinate2;
    myAnnotation2.title=@"Vaibhav";
    myAnnotation2.subtitle=@"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];
    temp.title=@"Back";
    self.navigationItem.backBarButtonItem=temp;
    //[temp release];
}

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

感谢帮助

更新:

地点.h

...
@interface Locations : NSManagedObject

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

更新 2:

这是我将注释“固定”的示例代码。

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

如何在数组上添加此注释?定义这个位置(坐标,标题,数组中的子标题并将它们添加到地图中。)

4

1 回答 1

0

位置实例将是您的注释

[mapView addAnnotations:locationArray];

位置.h

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

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

位置.m

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

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

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

这是你想要的吗?

于 2012-05-23T19:40:08.033 回答