如何将mapkit坐标从视图控制器(mapView)传递到详细控制器(mapView)

时间:2014-07-22 10:56:52

标签: ios objective-c xcode annotations mapkit

我有一个不同的经度和纬度的json文件,当我点击其中一个的注释时,我可以在我的mapView中显示它们,我可以去详细视图,在详细视图中我有一个小的地图视图,我想显示已点击的注释的坐标,

请您在此实施中帮助我,提前致谢!

感谢答案部分的任何代码!

以下是我的视图控制器中地图视图的实现:

- (void)viewDidLoad
 {
 [super viewDidLoad];

 _mapView.showsUserLocation = YES;
 _mapView.delegate = self;
    [self fetchData];
}



 -(void)fetchData
{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:BASED_URL]];
[request setHTTPMethod:@"GET"];
[request setValue:@"/json;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];

NSURLResponse *response;
NSData *GETReply = [NSURLConnection sendSynchronousRequest:request 
returningResponse:&response error:nil];
NSString *theReply = [[NSString alloc] initWithBytes:[GETReply bytes] length:[GETReply 
length] encoding: NSASCIIStringEncoding];


CLLocationCoordinate2D location;                         
NSMutableArray *newAnnotations = [NSMutableArray array]; 
NSError *error;
NSArray *array = [NSJSONSerialization JSONObjectWithData:GETReply
                                                 options:0
                                                   error:&error];
if (error != nil)
{
    // handle the error 
}

for (NSDictionary *dictionary in array)
{

    location.latitude = [dictionary[@"latitude"] doubleValue];
    location.longitude = [dictionary[@"longitude"] doubleValue];

    // create the annotation
    MyAnnotation *newAnnotation;


    newAnnotation = [[MyAnnotation alloc] init];
    newAnnotation.company = dictionary[@"company"];
    newAnnotation.coordinate = location;     
    [newAnnotations addObject:newAnnotation];
}
[self.mapView addAnnotations:newAnnotations];
}

以下是我将数据传递给详细视图控制器的方法:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
calloutAccessoryControlTapped:(UIControl *)control
 {


 MyAnnotation *annotation = view.annotation;

UIStoryboard *storyboard = self.navigationController.storyboard;

//the detail controller
DetailViewController *detail = [storyboard   
instantiateViewControllerWithIdentifier:@"DetailViewController"];

detail.companyData = annotation.company;
I don't know how to pass coordinate to mapView in detail view 

我的问题是如何传递坐标并在详细视图上的地图视图中显示

[self.navigationController pushViewController:detail animated:YES];

}

以下是我的详细视图的代码:

@synthesize companyData = _companyData;
@synthesize detailMapView = _detailMapView;

- (void)viewDidLoad
{
[super viewDidLoad];

_company.text = _companyData;

我不知道如何在__detailMapView

上添加它
} 

1 个答案:

答案 0 :(得分:1)

将属性添加到detailView的.h文件中:

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;

在callout-method中,添加以下内容:

detail.coordinate = annotation.coordinate;

并在detailView的viewDidLoad中使用coordinate-property:

    MyAnnotation *newAnnotation = [[MyAnnotation alloc] init];
    newAnnotation.coordinate = _coordinate;     
   [newAnnotations addObject:newAnnotation]
   [self.mapView addAnnotations:newAnnotations];