Unable to change MKMapView region(location)

拈花ヽ惹草 提交于 2019-12-12 04:56:15

问题


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:


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 :)



来源:https://stackoverflow.com/questions/29644267/unable-to-change-mkmapview-regionlocation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!