无法更改MKMapView区域(位置)

时间:2015-04-15 07:37:58

标签: iphone mkmapview mkcoordinateregion

我在我的应用程序中使用拆分视图控制器。左侧面板(主视图控制器)保存带有酒店列表的表视图,在右侧,详细视图控制器包含用于显示位置的MapView。最初我在应用程序启动时显示当前位置。然后从表视图中选择一个值后,我试图在地图中显示酒店位置。不幸的是,即使通过适当的纬度和经度值,我仍然无法改变地区,我根本看不到位置的任何变化。

以下是用于理解!!

的实现代码段
- (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];
}

以下是我如何利用位置值为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];
}

评论的行是我尝试的各种解决方案组合,但似乎没有任何效果。

注意:我能够准确传递坐标值,因为我在日志中找到了正确的值。

有人可以帮助我,谢谢:)

1 个答案:

答案 0 :(得分:1)

感谢安娜先生指出我朝着正确的方向前进。不是通过单例访问详细视图类或实例化类对象,而是使用 UISplitViewController的viewControllers 数组访问detailView然后进行更改,即:

- (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];
}

希望它有所帮助,谢谢:)