【问题标题】:How to pass mapkit coordinate from view controller(mapView) to detail controller (mapView)如何将 mapkit 坐标从视图控制器(mapView)传递到细节控制器(mapView)
【发布时间】:2014-07-22 10:56:51
【问题描述】:

我有一个具有不同经度和纬度的 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 

我的问题是如何传递坐标并将其显示在 detailview 的地图视图中

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

}

这是我的详细视图的代码:

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

- (void)viewDidLoad
{
[super viewDidLoad];

_company.text = _companyData;

不知道怎么添加到__detailMapView

} 

【问题讨论】:

  • 您需要将坐标数据存储在CLLocationCoordinate2D,并在另一个视图中管理
  • @iPatel 谢谢你的评论,如果你能在答案部分写你的例子,我很高兴
  • 在 DetailViewController 中,您已经声明了 companyData 属性。您是否尝试为坐标添加属性?当你尝试这样做时发生了什么?此外,您可以在 DetailViewController 中声明一个 annotation 属性并传递整个注释对象,这样 DetailViewController 就可以访问所有注释信息,因此您不必一次传递一个字段。

标签: ios objective-c xcode annotations mapkit


【解决方案1】:

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

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;

在标注方法中,添加以下内容:

detail.coordinate = annotation.coordinate;

并在您的 detailView 的 viewDidLoad 中使用坐标属性:

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

【讨论】:

  • 感谢您的回答,如果您也可以在detailView的viewDidLoad中显示坐标的使用部分
  • 您已经向我们展示了向 MKMapView 添加注释的代码,所以我想您已经知道它是如何工作的。但我已经更新了答案,希望对您有所帮助。
  • 是的,我是这样做的:MKPointAnnotation *point = [[MKPointAnnotation alloc] init]; point.coordinate = _coordinate; [_detailMapView addAnnotation:point];
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多