2016-01-13 34 views

回答

0

在你的地圖視圖的委託,做這個

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation 
{ 
    [mapView setCenterCoordinate:userLocation.location.coordinate animated:YES]; 
} 
+0

我dnd,t說我想放大用戶位置 –

1

對於那些有興趣在我設法做到這一點的解決方案。

首先,你需要添加你的手勢識別在viewDidLoad中

UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchOnMap:)]; 
[pinch setDelegate:self]; 
[pinch setDelaysTouchesBegan:YES]; 
[self.mapGestureContainer addGestureRecognizer:pinch]; 

UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(zoomInGesture:)]; 
gestureRecognizer.delegate = self; 
[gestureRecognizer setDelaysTouchesBegan:YES]; 
gestureRecognizer.numberOfTapsRequired = 2; 
[self.mapGestureContainer addGestureRecognizer:gestureRecognizer]; 

然後實現手勢功能,它會處理基礎上指開合縮放變焦。我添加了一個小的延遲以避免過多的處理。

-(void) zoomInGesture:(UITapGestureRecognizer *) recognizer { 
    currentRegion = self.mapView.region; 
    currentSpan = self.mapView.region.span; 
    isZoomingWithDoubleTap = YES; 

    MKCoordinateRegion region = currentRegion; 
    MKCoordinateSpan span = currentSpan; 

    span.latitudeDelta = currentSpan.latitudeDelta/2.3; 
    span.longitudeDelta = currentSpan.longitudeDelta/2.3; 
    region.span = span; 

    [self.mapView setRegion:region animated:YES]; 
} 

//Gesture used when the user pinch the area of the map 
- (void) pinchOnMap:(UIPinchGestureRecognizer *) recognizer { 

    if (recognizer.state == UIGestureRecognizerStateBegan) { 
     isPinching = YES; 
     self.mapView.scrollEnabled = NO; 
     self.mapView.userInteractionEnabled = NO; 

     currentRegion = self.mapView.region; 
     currentSpan = self.mapView.region.span; 
     lastZoomTime = [[NSDate date] timeIntervalSince1970]; 
    } 

    if (recognizer.state == UIGestureRecognizerStateEnded) { 
     isPinching = NO; 
     self.mapView.scrollEnabled = YES; 
     self.mapView.userInteractionEnabled = YES; 
    } 

    if (recognizer.state == UIGestureRecognizerStateChanged) { 
     if (([[NSDate date] timeIntervalSince1970] * 1000) - lastZoomTime >= 20) { 
      lastZoomTime = [[NSDate date] timeIntervalSince1970] * 1000; 

      MKCoordinateRegion region = currentRegion; 
      MKCoordinateSpan span = currentSpan; 
      span.latitudeDelta = currentSpan.latitudeDelta/recognizer.scale; 
      span.longitudeDelta = currentSpan.longitudeDelta/recognizer.scale; 
      region.span = span; 
      [self.mapView setRegion:region animated:NO]; 
     } 
    } 
}