2013-03-22 65 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