2013-03-22 63 views
1

我想获取当前位置并在MKMapView上为此坐标设置动画。我获得了当前位置,但是我没有在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]; 
} 
+0

要通过用户的当前位置.M我正确设置MapView的中心? – 2013-03-22 12:53:01

+0

是的,我想根据用户的当前位置设置mapview中心 – 2013-03-22 13:06:37

回答

1
self.mapView.userTrackingMode = MKUserTrackingModeFollow 

这将遵循一个蓝点,这似乎是要实现

+0

什么都没有发生。地图无法缩放到当前位置。 – 2013-03-22 09:12:53

+0

是你调用的位置委托方法吗?你在设备上还是在模拟器上运行? – wattson12 2013-03-22 15:35:49

+0

所有的代码在这里你可以看到,我在设备上运行。 – 2013-03-22 15:53:04

0

你必须为的MKMapView的userLocation.location财产的志愿通知注册什么用户位置。

要做到这一点,请将此代码放置在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]; 
    } 
} 
+0

它不起作用。地图仍然无法移动或缩放到用户的位置。 – 2013-03-26 09:39:44