无法更改MKMapView区域(位置) [英] Unable to change MKMapView region(location)

查看:107
本文介绍了无法更改MKMapView区域(位置)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是我利用位置值设置MapView区域的方法:

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

推荐答案

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

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

这篇关于无法更改MKMapView区域(位置)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆