Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何动态放置地图注释?_Objective C_Xcode_Mapkit - Fatal编程技术网

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

Objective c 如何动态放置地图注释?,objective-c,xcode,mapkit,Objective C,Xcode,Mapkit,我正在创建带有多个注释的地图,但我不知道动态性如何(我在CoreData中有它们) 我用这个代码修复了它们: - (void)viewDidLoad { [super viewDidLoad]; CLLocation *userLoc = mapView.userLocation.location; CLLocationCoordinate2D userCoordinate = userLoc.coordinate; NSLog(@"user latitud

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

下面是示例代码,我将注释放在“固定”位置


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

位置实例将是您的注释

[mapView addAnnotations:locationArray];
位置.h

...
@interface Locations : NSManagedObject

@property (nonatomic, retain) NSDecimalNumber * lat;
@property (nonatomic, retain) NSDecimalNumber * lon;
...
...
@interface Locations : NSManagedObject <MKAnnotation>{
    CLLocationCoordinate2D coordinate;
}

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

这就是您想要的吗?

位置实例将是您的注释

[mapView addAnnotations:locationArray];
位置.h

...
@interface Locations : NSManagedObject

@property (nonatomic, retain) NSDecimalNumber * lat;
@property (nonatomic, retain) NSDecimalNumber * lon;
...
...
@interface Locations : NSManagedObject <MKAnnotation>{
    CLLocationCoordinate2D coordinate;
}

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

这就是您想要的吗?

动态的含义是什么?在PHP中,我将它们放在如下位置:$array[]=$annotation;等等,还是不知道。请告诉我你到底想做什么。你能给我们展示一下核心数据类吗?你能创建一个数组并在其中添加几个注释吗?你的动态含义是什么?在PHP中,我这样放置:$array[]=$annotation;等等,还是不知道。请告诉我你到底想做什么。你能给我们展示一下核心数据类吗?你能创建一个数组并在其中添加几个注释吗?对不起,不行。我想在第一篇文章的代码上添加注释,但要在数组上添加注释。我更新了我的第一篇帖子。@crios你解决问题了吗?实际上,如果你只想创建一个数组并在其中添加几个注释。这很容易解决。现在我只是不知道如何动态地放置图像,所以今天我将在这里发布源代码ic,您可以看一看。对不起,不。我想在第一篇文章中的代码上添加注释,但在数组上。我更新了我的第一篇帖子。@crios你解决问题了吗?实际上,如果你只想创建一个数组并在其中添加几个注释。这很容易解决。现在我只是不知道如何动态地放置图像,所以我今天将在这里发布源代码ic,您可以看一下。