0
votes

I am using split view controller in my application. Left panel(Master View Controller) holds table view with list of hotels and on right, the Detail View Controller contains MapView for displaying the location. Initially I am displaying the current location upon application launch. Then after selecting a value from table view, I am trying to show the hotel location in the map. Unfortunately even upon passing proper latitude & longitude values, I am still not able to change the region and I don't see any change in the location at all.

Here is the implementation code snippet for understanding!!

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *hotelInfo = [self.hotelDetails objectAtIndex:indexPath.row];
    NSString *placeLatitude = [hotelInfo valueForKey:@"Latitude"];
    NSString *placeLongitude = [hotelInfo valueForKey:@"Longitude"];
    CLLocationCoordinate2D location;
    location.latitude = [placeLatitude doubleValue];
    location.longitude = [placeLongitude doubleValue];
    [self setMapViewLocation:location];
}

-(void)setMapViewLocation:(CLLocationCoordinate2D)location
{
    [[MapView sharedMapInstance] updateMapViewWithLocation:location];
}

Here is how I am utilising the location value for setting the region for MapView:

-(void)updateMapViewWithLocation:(CLLocationCoordinate2D)location
{
//    [self.locationManager startUpdatingLocation];
//    NSLog(@"latitude:%f,longitude:%f",location.latitude,location.longitude);
//    
//    MKCoordinateRegion region;
//    region.center = location;
//    
//    MKCoordinateSpan span;
//    span.latitudeDelta  = 0.015;
//    span.longitudeDelta = 0.015;
//    region.span = span;
//    NSLog(@"Center (%f, %f) span (%f, %f) user: (%f, %f)| IN!", region.center.latitude, region.center.longitude, region.span.latitudeDelta, region.span.longitudeDelta, location.latitude, location.longitude);
//
//    [self.locationView setRegion:region animated:YES];
//    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location, 800, 800);
//    MKCoordinateSpan span = {.latitudeDelta =  0.015, .longitudeDelta =  0.015};
    MKCoordinateRegion region;
    region.center = location;
    [self.locationView setRegion:[self.locationView regionThatFits:region] animated:NO];
}

The commented lines are the various combinations of solutions I tried, but nothing seems to work.

Note: I am able to pass the coordinate values precisely as I found correct values in log.

Can someone please help me, thanks :)

1
Are you using UISplitViewController? Is MapView sharedMapInstance a singleton? Using a singleton here seems a bit strange. How does sharedMapInstance give you a reference to the detail view controller of the UISplitViewController? - user467105
@Anna Yes, I am using UISplitViewController and sharedMapInstance is a singleton. static MapView *sharedMapInstance = nil; +(MapView *)sharedMapInstance { if (!sharedMapInstance) { sharedMapInstance = [[MapView alloc]init]; } return sharedMapInstance; } .Even if I instantiate detail view controller in didSelectRow method without singleton, still I don't see any change in region - Eshwar Chaitanya
How is the instance created by sharedMapInstance the same as the view controller instance used for the detail view controller in the split view controller? They must be two separate things. The singleton is just some vc you're creating in memory but is not the same as the one displayed in the detail side of the split vc. So changes you make to the singleton instance have no effect on the visible detail vc. - user467105
In didSelectRow method of table view of Master View, I instantiated MapView(Detail View Controller) and calling the method updateMapViewWithLocation: of detail view controller where I am passing the values to map view in the same. I am not setting location to Map view in Detail View through instance object in Master View, instead I am accessing a method of Detail View & then assigning values to detail view which holds map view in the same method. Pls see my code once again for better understanding :) - Eshwar Chaitanya
My point is: You don't need to and should not instantiate a new detail view controller instance. You have to get a reference to the detail view controller that is already instantiated and being displayed by the split view controller. In the master detail view controller, [self.splitViewController.viewControllers objectAtIndex:1] should be the reference to the detail vc. But rather than accessing the detail vc directly this way, it might be better to use NSNotificationCenter or some other loosely-coupled approach. - user467105

1 Answers

1
votes

Thanks to Mr.Anna for pointing me out in the right direction. Instead of accessing the detail view class through a singleton or instantiating the class object,use UISplitViewController's viewControllers array to access the detailView and then make changes, i.e.:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *hotelInfo = [self.hotelDetails objectAtIndex:indexPath.row];
    NSString *placeLatitude = [hotelInfo valueForKey:@"Latitude"];
    NSString *placeLongitude = [hotelInfo valueForKey:@"Longitude"];
    CLLocationCoordinate2D location;
    location.latitude = [placeLatitude doubleValue];
    location.longitude = [placeLongitude doubleValue];
    [self setMapViewLocation:location];
}

-(void)setMapViewLocation:(CLLocationCoordinate2D)location
{
    self.dvc = [self.splitViewController.viewControllers lastObject];
    MKCoordinateRegion region;
    region.center = location;
    MKCoordinateSpan span;
    span.latitudeDelta  = 0.015;
    span.longitudeDelta = 0.015;
    region.span = span;
    [self.dvc.locationView setRegion:[self.dvc.locationView regionThatFits:region] animated:NO];
}

Hope it helps, thanks :)