【问题标题】:Map camera position changes sharply without animation地图相机位置急剧变化,没有动画
【发布时间】:2013-03-19 11:09:31
【问题描述】:

我使用适用于 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天时间才这样做。现在寻求您的帮助。 提前谢谢你。

【问题讨论】:

    标签: xcode animation ios6 google-maps-sdk-ios


    【解决方案1】:

    我认为这样做的问题是调用setCamera 将捕捉到新的缩放级别而没有动画 - 所以下面对animateToCameraPosition 的调用实际上不会动画任何东西,因为相机已经在那个位置。

    但是,如果您删除了对setCamera 的调用,那么您可能会陷入无限循环,因为对animateToCameraPosition 的调用直到一段时间后才会真正更新相机(当动画播放时) ,以此类推,在循环的下一次迭代中,标记仍将全部可见。

    您需要做的是计算您需要的最终缩放级别,然后调用animateToCameraPosition 将相机移动到那里。

    我在这里发布了一些代码,用于计算相机的中心和缩放级别以适应边界:

    How to setRegion with google maps sdk for iOS?

    因此,您可以使用类似的方法来计算适合您的标记到屏幕上的相机位置。

    【讨论】:

    • 感谢您的公式。这正是我所需要的。
    【解决方案2】:

    更简单的答案:

    代替

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

    为我工作!

    【讨论】:

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

      【讨论】:

        【解决方案4】:

        此代码执行 mapview 相机到选定标记的平滑/动画重新定位。

        - (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker {
        
             [mapView animateWithCameraUpdate:[GMSCameraUpdate setTarget:marker.position]];
             mapView.selectedMarker = marker;
             return YES;
        }
        

        【讨论】:

        • 也许您可以提供有关您的答案的更多详细信息并解释您的代码打算做什么。
        猜你喜欢
        • 1970-01-01
        • 2015-03-27
        • 2015-08-22
        • 2014-07-08
        • 1970-01-01
        • 2013-06-12
        • 1970-01-01
        • 1970-01-01
        • 2017-02-03
        相关资源
        最近更新 更多