1

我想在 MKMapView 上获取当前位置并动画到这个坐标。我得到了当前位置,但我没有在 MKMapView 上对这个位置进行动画处理。如何使用蓝点动画到当前位置?

- (void)viewDidLoad
{
    [super viewDidLoad];

    locationManager = [[CLLocationManager alloc] init];

    self.mapView.delegate = self;
    [self.mapView setShowsUserLocation:YES];

    locationManager.delegate=self;
    locationManager.desiredAccuracy=kCLLocationAccuracyBest;
    locationManager.distanceFilter=kCLDistanceFilterNone;

    [locationManager startUpdatingLocation];
}

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

    location = [locationManager location];

    CLLocationCoordinate2D coord;
    coord.longitude = location.coordinate.longitude;
    coord.latitude = location.coordinate.latitude;
    lat = coord.latitude;
    longt = coord.longitude;

    [self.mapView setCenterCoordinate:coord animated:TRUE];
}

- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views

{
    MKAnnotationView *annotationView = [views objectAtIndex:0];
    id<MKAnnotation> mp = [annotationView annotation];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate] ,250,250);
    [mv setRegion:region animated:YES];
}
4

2 回答 2

1
self.mapView.userTrackingMode = MKUserTrackingModeFollow

这将使用蓝点跟随用户位置,这似乎是您想要实现的

于 2013-03-22T08:41:55.897 回答
0

您必须注册 MKMapView 的 userLocation.location 属性的 KVO 通知。

为此,请将此代码放在 ViewController 的 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];
    }
}
于 2013-03-25T04:51:53.937 回答