2011-11-24 95 views
15

我通過使用showUserLocation啓用了MKMapView中的current location。我還想將mapview居中放置到用戶當前的位置以及位置的放大地圖。請幫助我,因爲我沒有從其他類似的問題上獲得任何幫助。如何在MKMapView中居中當前位置?

我的代碼如下:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [mapView setMapType:MKMapTypeStandard]; 
    [mapView setZoomEnabled:YES]; 
    [mapView setScrollEnabled:YES]; 
    [mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES]; 
    [mapView setDelegate:self]; 
} 

回答

46

有關用戶所在地爲中心,你可以使用下面的代碼:

[mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES]; 

進行縮放的特殊位置,你應該學習如何在區(MKCoordinateRegion )進行計數和工作,計算您的區域值並使用呼叫顯示:

[mapView setRegion:myRegion animated:YES]; 

這個sample WorldCities顯示區域顯示的基本知識。

+0

我也doint同樣的事情,但它亙古不變的工作....如果可能的任何其他建議....在此先感謝... – vipul

+0

也許你可以發佈你的代碼在你的問題,所以這將很容易猜到什麼是錯=) – Denis

+0

我已經更新我的代碼有問題,這不會集中mapView currentLocation.Please檢查並幫助我....在此先感謝。 – vipul

2
MKCoordinateRegion region; 
    MKCoordinateSpan span; 
    span.latitudeDelta = 0.2;  // 0.0 is min value u van provide for zooming 
    span.longitudeDelta= 0.2; 

    CLLocationCoordinate2D location = [self addressLocation]; 
    region.span=span; 
    region.center =location;  // to locate to the center 
    if(addAnnotation != nil) { 
     [mapView removeAnnotation:addAnnotation]; 
     [addAnnotation release]; 
     addAnnotation = nil; 
    } 
    addAnnotation = [[AddressANnotation alloc] initWithCoordinate:location]; 
    [mapView addAnnotation:addAnnotation]; 
    [mapView setRegion:region animated:TRUE]; 
    [mapView regionThatFits:region]; 

這有助於我在地圖視圖中心顯示位置。

0

爲的MKMapView

self.mapView.showsUserLocation = YES; 
self.mapView.userTrackingMode = MKUserTrackingModeFollow; 
0
-(void) removeZoom:(float)deltaValue 
{ 
    MKCoordinateRegion region; 
    region.center.latitude = self.locationManager.location.coordinate.latitude; 
    region.center.longitude = self.locationManager.location.coordinate.longitude; 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.5]; 
    region.span.latitudeDelta = deltaValue; 
    region.span.longitudeDelta = deltaValue; 
    region = [mapViewSelf regionThatFits:region]; 
    [mapViewSelf setRegion:region animated:YES]; 
    [UIView commitAnimations]; 

} 
相關問題