【发布时间】: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