无法更改 MKMapView 区域(位置)
Unable to change MKMapView region(location)
我在我的应用程序中使用拆分视图控制器。左侧面板(主视图控制器)包含带有酒店列表的 table 视图,右侧的详细视图控制器包含用于显示位置的 MapView。最初我在应用程序启动时显示当前位置。然后从 table 视图中选择一个值后,我试图在地图中显示酒店位置。不幸的是,即使传递了正确的纬度和经度值,我仍然无法更改区域,而且我根本看不到位置有任何变化。
下面是实现代码片段供大家理解!!
- (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];
}
注释行是我尝试过的各种解决方案组合,但似乎没有任何效果。
注意:我能够准确地传递坐标值,因为我在日志中找到了正确的值。
谁能帮帮我,谢谢:)
感谢 Mr.Anna 为我指明了正确的方向。不是通过单例访问详细视图 class 或实例化 class 对象,而是使用 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];
}
希望对你有帮助,谢谢:)
我在我的应用程序中使用拆分视图控制器。左侧面板(主视图控制器)包含带有酒店列表的 table 视图,右侧的详细视图控制器包含用于显示位置的 MapView。最初我在应用程序启动时显示当前位置。然后从 table 视图中选择一个值后,我试图在地图中显示酒店位置。不幸的是,即使传递了正确的纬度和经度值,我仍然无法更改区域,而且我根本看不到位置有任何变化。
下面是实现代码片段供大家理解!!
- (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];
}
注释行是我尝试过的各种解决方案组合,但似乎没有任何效果。
注意:我能够准确地传递坐标值,因为我在日志中找到了正确的值。
谁能帮帮我,谢谢:)
感谢 Mr.Anna 为我指明了正确的方向。不是通过单例访问详细视图 class 或实例化 class 对象,而是使用 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];
}
希望对你有帮助,谢谢:)