Displaying MapView in next VC from Table view cell by taking Latitude and longitude keys from json?

111 views Asked by At

I am newbie to ios...please help me in solving this.i need to display map view in next view controller by tapping the row of a table view...

Here is my 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 station\r\nchaderghat","storeCity":"Hyderabad"}]

here is my LocationTable.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

Here is my 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;

    }

Method Declaration Of 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

my jsonactivity is get data from url into table View , but now I want to display that data (latitude and longitude) on map view in the next View Controller, How Could I do this ? please help me, thanks in advance !

1

There are 1 answers

0
Serge Maslyakov On

If the LocationViewController defined in a storyboard file, then you should create him via the next method:

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

Otherwise, all IBOutlet-objects will be a nil.