如何动态地放置地图注释?

时间:2012-05-23 19:17:43

标签: objective-c xcode mapkit

我正在创建带有多个注释的地图,但我不知道它是如何动态的(我在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];
}

P.S。如果您需要额外的代码,请告诉。

感谢您的帮助

更新

Locations.h

...
@interface Locations : NSManagedObject

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

更新2:

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

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

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

1 个答案:

答案 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";
}

这是你想要的吗?