代码之家  ›  专栏  ›  技术社区  ›  Danny Tuppeny

如果没有CLLocationManager,如何将MKMapView缩放到用户当前位置?

  •  69
  • Danny Tuppeny  · 技术社区  · 16 年前

    MKMapView 有一个名为“Show users current location”的选项,它将自动在屏幕上显示用户的位置 map

    我想移动和缩放到这个位置时,发现(如果它改变)。

    问题是,当用户位置在服务器上更新时,似乎没有调用任何方法 ,所以我没有地方放代码 zoom/scroll .

    MKMapView视图 CLLocationManager 我得到的更新与地图上的用户标记的更新不一致,因此在蓝色pin出现之前几秒钟,当我的地图移动和缩放时,看起来很傻。

    这感觉像是基本的功能,但我花了几个星期寻找解决方案,没有找到任何接近。

    6 回复  |  直到 13 年前
        1
  •  106
  •   ddnv    14 年前

    userLocation.location MKMapView .

    viewDidLoad: 或地图视图初始化位置的任何位置。

    [self.mapView.userLocation addObserver:self  
            forKeyPath:@"location"  
               options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)  
               context:NULL];
    

    然后实现此方法以接收KVO通知

    - (void)observeValueForKeyPath:(NSString *)keyPath  
                          ofObject:(id)object  
                            change:(NSDictionary *)change  
                           context:(void *)context {  
    
        if ([self.mapView showsUserLocation]) {  
            [self moveOrZoomOrAnythingElse];
            // and of course you can use here old and new location values
        }
    }
    

    这个代码对我来说很好用。
    self 在此上下文中是我的ViewController。

        2
  •  52
  •   MrHus    15 年前

    这是ddnv和Dustin对我有效的答案的结合:

    - (void) viewDidLoad
    {
        [self.mapView.userLocation addObserver:self 
                                    forKeyPath:@"location" 
                                       options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) 
                                       context:nil];
    }
    

    然后创建将地图移动到当前位置的实际列表方法:

    // Listen to change in the userLocation
    -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 
    {       
        MKCoordinateRegion region;
        region.center = self.mapView.userLocation.coordinate;  
    
        MKCoordinateSpan span; 
        span.latitudeDelta  = 1; // Change these values to change the zoom
        span.longitudeDelta = 1; 
        region.span = span;
    
        [self.mapView setRegion:region animated:YES];
    }
    

    不要忘记正确解除锁定并注销观察者:

    - (void)dealloc 
    {
        [self.mapView.userLocation removeObserver:self forKeyPath:@"location"];
        [self.mapView removeFromSuperview]; // release crashes app
        self.mapView = nil;
        [super dealloc];
    }
    
        3
  •  46
  •   Cœur Gustavo Armenta    9 年前

    自从iOS5.0以来,苹果为MKMapView添加了一个新方法。这种方法正是你想要的。

    https://developer.apple.com/documentation/mapkit/mkmapview

    - (void)setUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated;
    
        4
  •  13
  •   yonel    14 年前

    MKMapView 通过实现 MKMapViewDelegate 协议。只需实施:

    - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation      {
        CLLocationAccuracy accuracy = userLocation.location.horizontalAccuracy;
        if (accuracy ......) {
        } 
    }
    

    这个回调应该与地图上显示的内容完全同步。

        5
  •  6
  •   Kenan Karakecili    12 年前

    试试这个:

    [mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
    
        6
  •  1
  •   Dustin Pfannenstiel    16 年前

    没问题。。。在具有MKMapView的UIViewController子类的viewDidLoad方法中添加以下内容(假设MKMapView名为map):

    CLLocation *location = [[[CLLocation alloc] initWithLatitude:map.centerCoordinate.latitude longitude:map.centerCoordinate.longitude] autorelease]; //Get your location and create a CLLocation
    MKCoordinateRegion region; //create a region.  No this is not a pointer
    region.center = location.coordinate;  // set the region center to your current location
    MKCoordinateSpan span; // create a range of your view
    span.latitudeDelta = BASE_RADIUS / 3;  // span dimensions.  I have BASE_RADIUS defined as 0.0144927536 which is equivalent to 1 mile
    span.longitudeDelta = BASE_RADIUS / 3;  // span dimensions
    region.span = span; // Set the region's span to the new span.
    [map setRegion:region animated:YES]; // to set the map to the newly created region