【问题标题】:MKMapView resets after changing the view更改视图后 MKMapView 重置
【发布时间】:2014-02-01 08:10:12
【问题描述】:

MKMapView 有问题,在我将当前视图更改为另一个视图并再次返回地图后,它会一直重置。事实上,第一次一切顺利,地图正确地以用户位置为中心,但第二次就不行了。

这是我的代码:

MapViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

@interface MapViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>

@property (retain, nonatomic) IBOutlet MKMapView *mapView;

@end

MapViewController.m

#import "MapViewController.h"

@interface MapViewController ()

@end

MKMapView *mapView;

@implementation MapViewController

@synthesize mapView;

- (void)mapView:(MKMapView *)aMapView didUpdateUserLocation:(MKUserLocation *)aUserLocation
{
    MKCoordinateRegion region;
    MKCoordinateSpan span;
    span.latitudeDelta = 0.009;
    span.longitudeDelta = 0.009;
    CLLocationCoordinate2D location;
    location.latitude = aUserLocation.coordinate.latitude;
    location.longitude = aUserLocation.coordinate.longitude;
    region.span = span;
    region.center = location;

    [aMapView setRegion:region animated:YES];
}

- (void)viewDidLoad
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        CGSize result = [[UIScreen mainScreen] bounds].size;

        if (result.height == 480)
        {
            mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 64, 320, 416)];
            mapView.showsUserLocation = YES;
            mapView.mapType = MKMapTypeHybrid;
            mapView.delegate = self;
        }

        if (result.height == 568)
        {
            mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 64, 320, 504)];
            mapView.showsUserLocation = YES;
            mapView.mapType = MKMapTypeHybrid;
            mapView.delegate = self;
        }
    }

    [self.view addSubview:mapView];

    [super viewDidLoad];
}

@end

还有一些图片

第一次:

第二次:

感谢大家帮我解决这个问题,祝你有美好的一天!

【问题讨论】:

  • 好的,我知道了。你是在说缩放级别吗?
  • 当您移出视图时,请使用 map.center 存储缩放级别和位置坐标。然后当您再次返回时,将地图中心和缩放级别设置为它。
  • 创建一个不同的方法来更新区域,并将其添加到 MKCoordinateRegion 区域; MKCoordinateSpan 跨度; span.latitudeDelta = 0.009; span.longitudeDelta = 0.009; CLLocationCoordinate2D位置; location.latitude = self.yourmapview.userlocation.coordinate.latitude; location.longitude = self.yourmapview.userlocation.coordinate.longitude; region.span = 跨度; region.center = 位置; [self.yourMapView setRegion:region Animation:YES];在做完你已经在做的事情之后,在 viewdidload 中调用它。在 didUpdateUserLocation 中也调用相同的
  • 1) 为什么在@implementation 上方的.m 文件中声明了另一个全局mapView?你不需要它。 2)在.h中,您有一个标记为IBOutlet的mapView,因此您将其连接到xib中的地图视图,对吗?那么为什么要在 viewDidLoad 中创建并添加一个新的呢? 3) 这是all MapViewController.m 中的代码吗?您是否对 .m 文件中的 mapView 进行了任何操作? 4) 您如何准确地“将当前视图更改为另一个视图并返回”?你能显示那个代码吗?
  • 代理出口应该连接到底部的橙色圆圈图标(红色立方体左侧的图标)。然后注释掉整个 viewDidLoad 方法。

标签: ios xcode mkmapview mapkit core-location


【解决方案1】:

创建不同的方法来更新区域

-(void)yourMethod{

MKCoordinateRegion region; 
MKCoordinateSpan span; 
span.latitudeDelta = 0.009; 
span.longitudeDelta = 0.009; 
CLLocationCoordinate2D location; 
location.latitude = self.yourmapview.userlocation.coordinate.latitude; 
location.longitude = self.yourmapview.userlocation.coordinate.longitude; 
region.span = span; 
region.center = location; 
[self.yourMapView setRegion:region animated:YES]; 
}

-(void)viewWillAppear{
//After [self.view addSubview:mapView];
[self yourMethod];
}

试一试,但现在不要更新您的“didUpdateUserLocation”。如果您在“viewWillAppear”方法中的“yourMapView.location”中有值,请告诉我。

【讨论】:

  • 感谢您的热心帮助,但是您的代码不起作用,实际上结果与以前相同:-/。我没有为“mapView.location”设置值。另外,按照您的代码,我是否必须将“viewDidLoad”替换为“viewWillAppear”?
  • 您可以使用 mapview.userlocation 。 viewdidload 和 viewwillappear 也是两种不同的方法。您不能简单地将一个替换为另一个。
  • 再次感谢您,我已经尝试了另一次 [使用 viewWillAppear,用 viewDidLoad 替换它,反之亦然],但它仍然不起作用 :-(。我不知道是哪个问题 :- /
【解决方案2】:

我翻阅了文档,发现了一个 mapView 属性 visibleMapRect
当您推送新视图时,将此值存储为类属性。 例如:

@property MkMapRect currentRect;

初始化currentRect = CGRectZero;
在推送新视图之前,

currentRect = mapView.visibleMapRect;

当您稍后进入此视图时,将调用 mapView 委托并在其中一个委托方法中编写以下代码

if (currentRect.size.width!=0)
  [mapView setVisibleMapRect:currentRect animated:YES].

它会起作用的。

【讨论】:

  • 你能更好地给我解释一下吗?例如使用我在问题中发布的代码?我不知道把你写的那些行放在哪里:-/
  • visibleMapRect 属性保存 mapView 的位置和缩放级别。因此,当您从 mapView 移动到另一个 View 时,您会存储此值。当您从 otherView 回到 mapView 时,使用此设置 mapView 的区域方法,以便您的地图视图将显示最后一个位置。
  • 再次感谢您,但您能否在我在问题中发布的代码中写一些代码,以便我更好地理解我必须将这些代码行放在哪里?我是一名业余程序员:-/。非常感谢您抽出宝贵时间@santhu :-)
猜你喜欢
  • 1970-01-01
  • 2011-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-04
  • 2016-08-22
  • 1970-01-01
相关资源
最近更新 更多