我使用谷歌地圖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天時間來做這件事。現在請求你的幫助。 預先感謝您。
感謝您的公式。這是我需要的。 – Geek 2013-03-19 16:19:18