2011-11-24 31 views
15

Tôi đang hiển thị số current location trong MKMapView bằng cách sử dụng showUserLocation cho phép. Tôi cũng muốn căn giữa mapview với vị trí hiện tại của người dùng cũng như bản đồ vị trí được thu phóng. Xin hãy giúp tôi về điều này vì tôi không nhận được bất kỳ sự giúp đỡ từ các câu hỏi tương tự khác trên stackoverflow.Làm cách nào để căn giữa vị trí hiện tại của tôi trong MKMapView?

Mã của tôi là dưới đây:

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

Trả lời

46

Đối với trung vào vị trí người dùng, bạn có thể sử dụng đoạn mã sau:

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

Đối phóng to trên địa điểm đặc biệt, bạn nên học cách vùng (MKCoordinateRegion) được tính và làm việc, đếm các giá trị của bạn cho khu vực và hiển thị giá trị đó cho khu vực và hiển thị giá trị đó bằng cách gọi:

[mapView setRegion:myRegion animated:YES]; 

Điều này hiển thị sample WorldCities thông tin cơ bản về hiển thị khu vực.

+0

i am cũng doint điều tương tự nhưng nó doesnot công việc .... Bất kỳ lời đề nghị khác nếu có thể .... cảm ơn trước ... – vipul

+0

có lẽ bạn có thể đăng bài mã của bạn trong câu hỏi của bạn, vì vậy nó sẽ dễ dàng để đoán những gì sai =) – Denis

+0

Tôi đã cập nhật mã của tôi trong câu hỏi và điều này sẽ không tập trung mapView để currentLocation.Xin vui lòng kiểm tra nó và giúp tôi .... cảm ơn trước. – 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]; 

Điều này đã giúp tôi hiển thị vị trí ở trung tâm của chế độ xem bản đồ.

0

cho 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]; 

} 
Các vấn đề liên quan