xcode 地图相机位置在没有动画的情况下急剧变化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15497827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:00:43  来源:igfitidea点击:

Map camera position changes sharply without animation

xcodeanimationios6google-maps-sdk-ios

提问by Geek

I use google Maps SDK for iOS. I have markers on my map and want to set proper zoom so that markers are separate and also markers should be visible on screen all at once. I am able to do it.I have done using logic that if all markers are visible then zoom in. But the problem is that map camera is changing sharply. I tried a lot to animate this zoom but I failed to do to. I tried animateToCameraPosition, animateToZoom and UIView animation method.

我使用适用于 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]];
        }
    }
}

I also tried reversing order of setting mapview camera and animating camera in while loop of if condition. I spent 3 days just doing it. Now asking for your help. Thank you in advance.

我还尝试在 if 条件的 while 循环中颠倒设置 mapview 相机和动画相机的顺序。我花了 3 天的时间才这样做。现在请求您的帮助。先感谢您。

回答by Saxon Druce

I think the problem with this is that calling setCamerawill snap to the new zoom level without animating - and so the following call to animateToCameraPositionwon't actually animate anything, as the camera is already at that position.

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

However, if you removed the call to setCamerathen you'd probably end up in an infinite loop, as the call to animateToCameraPositionwon't actually update the camera until some time later (as the animation plays), and so on the next iteration of the loop the markers would still all be visible.

但是,如果您删除了对setCamerathen的调用,那么您可能最终会陷入无限循环,因为animateToCameraPosition直到一段时间后(随着动画播放),对 的调用才会实际更新相机,依此类推循环标记仍然全部可见。

What you need to do is calculate the final zoom level that you need, and then make a single call to animateToCameraPositionto move the camera there.

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

I posted some code for calculating the centre and zoom level for a camera to fit a bounds here:

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

How to setRegion with google maps sdk for iOS?

如何使用适用于 iOS 的谷歌地图 sdk 设置区域?

So you could use something similar to calculate a camera position which would fit your markers to the screen.

所以你可以使用类似的东西来计算一个适合你的标记到屏幕上的相机位置。

回答by stackOverFlew

Much simpler answer:

更简单的答案:

Instead of

代替

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

Do this:

做这个:

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

Works for me!

对我有用!

回答by Sajid Israr

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

回答by timurbeg

This code performs smooth/animated repositioning of mapview's camera to the selected marker.

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

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

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