【问题标题】:iOS: How can I refresh MKMapView user location everytime the user loads the appiOS:每次用户加载应用程序时如何刷新 MKMapView 用户位置
【发布时间】:2014-01-17 02:24:12
【问题描述】:

我正在实现 MKMapView,并且在用户第一次加载应用程序时工作正常,但之后即使用户从后台返回,它也不会刷新用户位置。

我的这部分代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.mapView.delegate=self;
    self.mapView.zoomEnabled = YES;
    self.mapView.userTrackingMode = MKUserTrackingModeFollow;
    self.mapView.zoomEnabled = YES;
    self.mapView.showsUserLocation = YES;
}

我尝试在 viewWillAppear 中实现上面的代码,看看它是否会在用户每次加载应用时重新加载,但它不起作用。

你们中的任何人都知道如何在每次应用加载时重新加载地图视图?

非常感谢您的帮助。

【问题讨论】:

    标签: ios iphone mkmapview xcode5


    【解决方案1】:

    您可以为此使用以下委托方法:

    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
    }
    

    【讨论】:

      【解决方案2】:

      有点不清楚您所说的“不刷新用户位置”和“重新加载”是什么意思,所以我假设您想要在重新加载时将地图重新​​置于用户当前位置的中心。

      首先,要触发事物,您需要 AppDelegate applicationDidBecomeActive: 或 applicationWillEnterForeground: 方法中的代码。这是您可以启动操作的“触发”通知。

      然后,要重新定位当前位置,例如:

      - (void) _putMapToCurrentlLocation
      {
          CLLocation* location = [[LocationManager sharedInstance] currentLocation];
      
          MKCoordinateRegion newRegion;
          newRegion.span.latitudeDelta = 0.13;
          newRegion.span.longitudeDelta = 0.12;
          newRegion.center.latitude = location.coordinate.latitude - (0.2*newRegion.span.latitudeDelta);
          newRegion.center.longitude = location.coordinate.longitude;
      
          [self.mapView setRegion:newRegion animated:YES];
      
          self.mapView.showsBuildings = YES;
          self.mapView.showsPointsOfInterest = YES;
          self.mapView.showsUserLocation = YES;
      }
      

      注意:“LocationManager”类是我的一个单独的类,它只处理从 iOS 注册位置通知并保持最近报告的位置。如果您需要帮助,这是一个完整的问题...... :-)

      【讨论】:

        【解决方案3】:

        您的解决方案是通知。尝试实现以下

        //appdelegate.m
        - (void)applicationDidBecomeActive:(UIApplication *)application
        {
           if([[UIApplication sharedApplication] applicationState]==UIApplicationStateActive)
           {
                 [[NSNotificationCenter defaultCenter] postNotificationName:@"applicationEnterForeground" object:self];
           }
        }
        
        //yourViewController.m
        - (void)viewDidLoad
        {
           [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadMapView) name:@"applicationEnterForeground" object:nil];
        }
        
        -(void)reloadMapView
        {
            NSLog(@"Reload your map view here");
        }
        

        【讨论】:

          【解决方案4】:

          在您的 ViewController 的 viewDidLoad 方法中,如果用户再次加载应用程序并在方法中编写代码以刷新地图视图,请添加一个观察者

          [[NSNotificationCenter defaultCenter]addObserver:self
                                                   selector:@selector(yourMethod)
                                                       name:UIApplicationDidBecomeActiveNotification
                                                     object:nil];
          

          在dealloc方法中移除观察者

          - (void) dealloc {
              [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:Nil];
          }
          

          【讨论】:

            猜你喜欢
            • 2021-06-10
            • 1970-01-01
            • 2018-04-03
            • 2011-08-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-01-29
            相关资源
            最近更新 更多