目标c-通过从json中获取纬度和经度键,在下一个VC的表视图单元格中显示MapView



我是ios新手。。。请帮我解决这个问题。我需要在下一个视图控制器中通过点击表视图的行来显示地图视图。。。

这是我的json:

[{"storeId":"STORE001","name":"Creamstone","latitude":"17.441934745594924","longitude":"78.45833734609187","storeAddress":"Begumpet, OPP: HPS","storeCity":"Hyderabad"},{"storeId":"STORE002","name":"Cream Stone","latitude":"17.37732708258546","longitude":"78.49484514445066","storeAddress":"near malakpet railway stationrnchaderghat","storeCity":"Hyderabad"}]

这是我的位置表.m

                #import "LocationTable.h"
                #import "LocationViewController.h"
                #import "Stores.h"
                @interface LocationTable ()
                {
                    NSDictionary *coordinates;
                }
                @end
                #define getStores  @"http://192.168.28.9:8080/FoodCart/rest/storersrc/storebycity/hyderabad"
                @implementation LocationTable
                @synthesize json,locations;
                 - (void)viewDidLoad {
                    [super viewDidLoad];
                    self.title = @"Locations";
                    [self.tableView reloadData];
                    [self retrieveData];
                }
                - (void)didReceiveMemoryWarning {
                    [super didReceiveMemoryWarning];
                  }
               - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView                 {
                    // Return the number of sections.
                    return 1;
                }
                - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
                    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LocationCell"];
                    Stores *location = [locations objectAtIndex:indexPath.row];
                    cell.textLabel.text = location.name;
                    cell.detailTextLabel.text = location.storeAddress;
                   return cell;
                }

            -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
                    LocationViewController*lvc = [[LocationViewController alloc] init];
                    Stores *currentlocation = [locations objectAtIndex:indexPath.row];
                    lvc.latitude= currentlocation.latitude;
                    lvc.longitude=currentlocation.longitude;
                       [self.navigationController pushViewController:lvc animated:YES];
                }
                - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
                    return locations.count;
                }
                -(void)retrieveData
                {
                    NSError*error;
                    NSURL * url = [NSURL URLWithString:getStores];
                    NSData*data = [NSData dataWithContentsOfURL:url];
                    json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
                    locations = [[NSMutableArray alloc] init];
                    for(int i=0;i<json.count;i++)
                    {
                        NSString * cStoreId = [[json objectAtIndex:i] objectForKey:@"storeId"];
                        NSString * cName = [[json objectAtIndex:i] objectForKey:@"name"];
                        NSNumber * cLatitude = [[json objectAtIndex:i] objectForKey:@"latitude"];
                        NSNumber * cLongitude = [[json objectAtIndex:i] objectForKey:@"longitude"];
                        NSString * cStoreAddress = [[json objectAtIndex:i] objectForKey:@"storeAddress"];
                        Stores *mylocations = [[Stores alloc] initWithstoreId:(NSString *) cStoreId andname:(NSString *) cName andlatitude:(NSNumber *) cLatitude andlongitude:(NSNumber *) cLongitude andstoreAddress:(NSString *) cStoreAddress];
                        [locations addObject:mylocations];
                    }
                }
        @end

这是我的LocationViewController.m:

    #import "LocationViewController.h"
    #import "MapView.h"
    #import "LocationTable.h"
    @interface LocationViewController ()
    @property (nonatomic,strong) NSNumber *latitude;
    @property (nonatomic,strong) NSNumber *longitude;
    @property (nonatomic,strong) IBOutlet MKMapView *Mapview;
    @end
    @implementation LocationViewController
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.Mapview.delegate = self;
        MapView *mapPoint = [[MapView alloc] init];
        mapPoint.coordinate = CLLocationCoordinate2DMake([self.Stores.latitude doubleValue], [self.Stores.longitude doubleValue]);
        // Add it to the map view
        [self.Mapview addAnnotation:mapPoint];
        // Zoom to a region around the pin
        MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(mapPoint.coordinate, 500, 500);
        [self.Mapview setRegion:region];
        self.title = self.Stores.name;
    }

MKAnnotationView 的方法声明

     -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
        MKPinAnnotationView *view = nil;
        static NSString *reuseIdentifier = @"MapView";
         view = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIdentifier];
        if(!view) {
            view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
            view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            view.canShowCallout = YES;
            view.animatesDrop = YES;
        }
          return view;
    }
    @end

我的jsonactivity是将数据从url获取到表View中,但现在我想在下一个View Controller的地图视图中显示这些数据(纬度和经度),我该怎么做?请帮帮我,提前谢谢!

如果LocationViewController在情节提要文件中定义,那么您应该通过下一个方法创建它:

[[UIStoryboard storyboardWithName:<your_storyboard_name> bundle:nil] instantiateViewControllerWithIdentifier:<your_controller_identifier>];

否则,所有IBOutlet对象都将为零。

最新更新