我从 tableViewController 开始,一旦我点击一个单元格,它就会弹出一个 mapView,它执行我在 MapViewController 中声明的方法:
MapViewController *category1 = [[MapViewController alloc] init];
NSArray *array=[self.navigationController viewControllers];
[category1 categoryDining];
[self.navigationController popToViewController:[array objectAtIndex:[array count]-2] animated:YES];
这工作得很好。它弹出到 mapViewController,并执行以下方法:
-(void)categoryDining{
//user's location
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[locationManager startUpdatingLocation];
CLGeocoder *fgeo = [[CLGeocoder alloc] init];
[fgeo reverseGeocodeLocation:locationManager.location completionHandler:^(NSArray *placemarks, NSError *error) {
if (!error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
zip = placemark.postalCode;
NSLog(@"%@", zip);
// Make the URL connection in order to download JSON/XML data.
NSString *stringSearch = @"http://api.onebigplanet.com/ws/local-daily-deals?wsToken=&wsVersion=3.0.0&numItems=30&out=json&categoryId=11&radiusInMiles=100&zipCode=";
NSString *combined = [NSString stringWithFormat:@"%@%@", stringSearch, zip];
NSLog(@"%@",combined);
NSURL *url = [NSURL URLWithString:combined];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
// Error and success message handling.
[NSURLConnection
sendAsynchronousRequest:urlRequest
queue:queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if([data length] > 0 &&
error == nil){
NSData *jsonData = data;
if (jsonData != nil){
NSError *error = nil;
self.result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
if(error == nil)
self.mapLocations = result;
//NSLog(@"%@", result);
for (NSDictionary *location in self.mapLocations[@"merchants"]) {
for (NSDictionary *locations in location[@"branches"]) {
NSLog(@"%@",locations);
CLLocationCoordinate2D annotationCoordinate =
CLLocationCoordinate2DMake([locations[@"latitude"] doubleValue],
[locations[@"longitude"] doubleValue]);
Annotation *annotation = [[Annotation alloc] init];
annotation.coordinate = annotationCoordinate;
annotation.title = locations[@"name"];
annotation.subtitle = locations[@"street"];
[self.mapView addAnnotation:annotation];
}
}
}
else if ([data length] == 0 &&
error == nil){
NSLog(@"Nothing was downloaded");
}
else if (error != nil){
NSLog(@"Error happened = %@", error);
}
}
}];
}
}];
}
我已经通过注销结果对其进行了测试,但它们(标记/图钉)不会显示在地图上。出于某种原因,我认为当我从另一个 viewController 调用方法时,不会为该方法调用 ViewDidLoad 和 ViewWillLoad 方法。我想我需要与某种委派合作,但有点不确定如何去做。任何帮助将不胜感激。