2011-05-01 14 views
0
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views { 
for(MKAnnotationView *annotationView in views) { 
    if(annotationView.annotation == mv.userLocation) { 
     MKCoordinateRegion region; 
     MKCoordinateSpan span; 

     span.latitudeDelta=0.002; 
     span.longitudeDelta=0.002; 

     CLLocationCoordinate2D location=mv.userLocation.coordinate; 

     region.span=span; 
     region.center=location; 

     [mv setRegion:region animated:TRUE]; 
     [mv regionThatFits:region]; 
    } 
} 

大家好。我曾嘗試搜索其他帖子和網站。實質上我想複製'當前位置'按鈕。'當前位置'按鈕iPhone中的地圖複製品

使用上面的代碼,我可以放大用戶當前的位置。然而,這並不完美,有時GPS更新有點遲,或者認爲我在別的地方,會放大到那個位置,但是藍點會從屏幕的某個地方移到我真正的位置。

我想知道有沒有一種方法可以添加「按鈕」,就像iphone上的地圖一樣。它不需要獲得新的縮放等,只需移動到新的更新的位置。我的主要代碼來源幾乎是here

我很感激任何幫助。

回答

0

使用相同的圖形創建自定義註釋。該對象必須實現MKAnnotationView,並且您必須在MKMapViewDelegate的方法mapView:viewForAnnotation:中返回自定義視圖。如果您需要確切的圖形,請使用項目UIKit-Artwork-Extractor來獲取它。

保存到註解視圖的引用,訂閱核心位置更新,並且更新位置調用類似:

- (void) animateView:(UIView *)view toPosition:(NSValue*)value 
{ 
CGPoint newCenter = [value CGPointValue]; 
[UIView animateWithDuration:0.5 
    animations:^{ 
     view.center = newCenter; 
    } 
    completion:^(BOOL finished){ }]; 
} 

然後設置上註釋的地圖的中心:

- (void)centerMap:(CLLocation *)location { 
    MKCoordinateRegion region = { { 0.0f, 0.0f }, { 0.0f, 0.0f } }; 
    region.center = location.coordinate; 
    region.span.longitudeDelta = 0.15f; 
    region.span.latitudeDelta = 0.15f; 
    [self.mapView setRegion:region animated:YES]; 
} 
相關問題