我使用适用于 iOS 的谷歌地图 SDK。我的地图上有标记,并且想要设置适当的缩放比例,以便标记是分开的,并且标记应该同时在屏幕上可见。我能够做到。我已经使用逻辑完成,如果所有标记都可见然后放大。但问题是地图相机正在急剧变化。我尝试了很多动画这个缩放,但我没能做到。我尝试了 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]];
}
}
}
我还尝试在 if 条件的 while 循环中颠倒设置 mapview 相机和动画相机的顺序。我花了3天时间才这样做。现在寻求您的帮助。先感谢您。