2013-03-19 49 views
9

我使用谷歌地圖SDK的iOS。 我在我的地圖上有標記,並且想要設置適當的縮放比例,以便標記是分開的,標記也應該一次顯示在屏幕上。 我可以做到這一點。我已經使用邏輯,如果所有標記都可見然後放大。但問題是地圖相機正在急劇變化。我嘗試了很多動畫來縮放這個縮放,但我沒有做到。我嘗試了animateToCameraPosition,animateToZoom和UIView動畫方法。沒有動畫的地圖相機位置急劇變化

- (BOOL)allCoordinatesVisibleWithLatitudes:(NSArray *)arrayOfLatitudes Longitudes:(NSArray *)arrayOfLongitudes 
    { 
    CLLocationCoordinate2D coordinate; 
    for(int i=0;i<arrayOfLatitudes.count;i++) 
    { 
     coordinate = CLLocationCoordinate2DMake([[arrayOfLatitudes objectAtIndex:i] floatValue], [[arrayOfLongitudes objectAtIndex:i] floatValue]); 
     if(![self.mapView.projection containsCoordinate:coordinate]) 
     { 
      return NO; 
     } 
    } 
    return YES; 
} 


- (void)setZoomToHaveProperView 
{ 
    NSMutableArray * arrayOfLatitudes = [[NSMutableArray alloc] init]; 
    NSMutableArray * arrayOfLongitudes = [[NSMutableArray alloc] init]; 
    RS_SingleMatch * singleMatch = [[RS_SingleMatch alloc] init]; 
    float zoom = 0.0; 

    // You may ignore this first for loop. it just takes coordinate of all markers in  arrays. 

    for(int i=0;i<=self.userMatches.arrayOfMatches.count;i++) 
    { 
     if(i==self.userMatches.arrayOfMatches.count) 
     { 
      RS_Trip * userTrip = [[RS_Trip alloc] init]; 
      [arrayOfLatitudes addObject:[NSNumber numberWithFloat:userTrip.fromLat]]; 
      [arrayOfLongitudes addObject:[NSNumber numberWithFloat:userTrip.fromLon]]; 
      if(self.segmentedControl.selectedSegmentIndex == 1) 
      { 
       [arrayOfLatitudes addObject:[NSNumber numberWithFloat:userTrip.toLat]]; 
       [arrayOfLongitudes addObject:[NSNumber numberWithFloat:userTrip.toLon]]; 
      } 
     } 
     else 
     { 
      singleMatch = [self.userMatches.arrayOfMatches objectAtIndex:i]; 
      [arrayOfLatitudes addObject:[NSNumber numberWithFloat:singleMatch.fromLat]]; 
      [arrayOfLongitudes addObject:[NSNumber numberWithFloat:singleMatch.fromLong]]; 
      if(self.segmentedControl.selectedSegmentIndex == 1) 
      { 
       [arrayOfLatitudes addObject:[NSNumber numberWithFloat:singleMatch.toLat]]; 
       [arrayOfLongitudes addObject:[NSNumber numberWithFloat:singleMatch.toLong]]; 
      } 
     } 
} 
    zoom = self.mapView.camera.zoom; 

    // if all coordinates are visible then zoom in 
    if([self allCoordinatesVisibleWithLatitudes:arrayOfLatitudes Longitudes:arrayOfLongitudes]) 
    { 
     while ([self allCoordinatesVisibleWithLatitudes:arrayOfLatitudes Longitudes:arrayOfLongitudes]) 
     { 
      zoom += 0.5; 
      [self.mapView setCamera:[GMSCameraPosition cameraWithTarget:self.mapView.camera.target zoom:zoom]]; 
      [self.mapView animateToCameraPosition:[GMSCameraPosition cameraWithTarget:self.mapView.camera.targetAsCoordinate zoom:zoom]]; 
     } 
    } 
} 

我也試着倒車設置MapView的攝像頭,並在if條件while循環動畫攝像機的順序。我花了3天時間來做這件事。現在請求你的幫助。 預先感謝您。

回答

12

我認爲這個問題是,調用setCamera會捕捉到新的縮放級別沒有動畫 - 所以下面的調用animateToCameraPosition實際上不會動畫任何東西,因爲相機已經在那個位置。

但是,如果您刪除了對setCamera的呼叫,那麼最終可能會以無限循環結束,因爲animateToCameraPosition的呼叫在一段時間後纔會實際更新攝像頭(如動畫播放),等等在循環的下一次迭代中,標記仍將全部可見。

您需要做的是計算您需要的最終縮放級別,然後撥打animateToCameraPosition來移動相機。

我張貼一些代碼用於計算相機的中心和縮放級別來這裏適應範圍:

How to setRegion with google maps sdk for iOS?

所以,你可以使用類似的東西來計算這將滿足您的標記,以攝像機位置屏幕。

+0

感謝您的公式。這是我需要的。 – Geek 2013-03-19 16:19:18

9

簡單多了回答:

相反

[self.mapView setCamera:[GMSCameraPosition cameraWithTarget:self.mapView.camera.target zoom:zoom]]; 
[self.mapView animateToCameraPosition:[GMSCameraPosition cameraWithTarget:self.mapView.camera.targetAsCoordinate zoom:zoom]]; 

做到這一點:

[self.mapView animateToCameraPosition:[GMSCameraPosition cameraWithTarget:[GMSCameraPosition cameraWithTarget:self.mapView.camera.target zoom:zoom]]]; 

對我的作品!

2

該代碼執行mapview相機的平滑/動畫重定位到選定的標記。

- (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker { 

    [mapView animateWithCameraUpdate:[GMSCameraUpdate setTarget:marker.position]]; 
    mapView.selectedMarker = marker; 
    return YES; 
} 
+0

也許你可以給出更多關於你的答案的細節,並解釋你的代碼打算做什麼。 – rducom 2015-02-18 22:44:23

+0

已添加評論 – timurbeg 2015-02-19 15:49:43

3
GMSCameraPosition *newCameraPosition = [GMSCameraPosition cameraWithTarget:cordinate zoom:position.zoom]; 
dispatch_async(dispatch_get_main_queue(), ^{ 
     [self.mapView animateToCameraPosition:newCameraPosition]; 
});