我在应用程序中使用拆分视图控制器。左侧面板(主视图控制器)包含带有酒店列表的表格视图,右侧的局部视图控制器包含用于显示位置的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];
}
评论的行是我尝试过的各种解决方案的组合,但似乎都不起作用。
注意:我能够准确地传递坐标值,因为我在日志中找到了正确的值。
有人能帮我吗,谢谢:)
感谢安娜先生为我指明了正确的方向。使用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];
}
希望有帮助,谢谢:)