将GMSMapView缩放以适应GMSMarker标记?

4

这段代码让我期望可以将所有的标记放在地图上并使 GMSMapView 自适应缩放以适配我的所有 GMSMarker,我使用了 GMSCoordinateBounds 类中的 includingCoordinate 方法来实现这个功能,但结果并不如我所愿。

以下是我的示例:

#import "ViewController.h"

@import GoogleMaps;
@import CoreLocation;

@interface ViewController ()

@property (weak, nonatomic) IBOutlet GMSMapView *mapView;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSMutableArray <CLLocation *> *locations = [NSMutableArray array];
    [locations addObject:[[CLLocation alloc] initWithLatitude:55.755786 longitude:37.617633]];
    [locations addObject:[[CLLocation alloc] initWithLatitude:-22.9035393 longitude:-43.2095869]];
    [locations addObject:[[CLLocation alloc] initWithLatitude:51.50998 longitude:-0.1337]];

    NSArray *titles = @[@"marker1", @"marker2", @"marker3"];

    NSMutableArray <GMSMarker *> *markers = [NSMutableArray array];

    for (NSUInteger i = 0; i < 3; ++i)
    {
        GMSMarker *marker = [[GMSMarker alloc] init];
        marker.position = locations[i].coordinate;
        marker.title = titles[i];
        marker.map = _mapView;
        [markers addObject:marker];
    }

    GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithCoordinate:markers[0].position coordinate:markers[0].position];

    for (GMSMarker *marker in markers)
    {
        bounds = [bounds includingCoordinate:marker.position];
    }

    [_mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds]];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end

这是此代码的结果:

输入图像描述


也许这可以帮助:https://dev59.com/X2445IYBdhLWcg3w0ddD - Wolverine
@Krupanshu 我使用的是 Google 地图,而不是 MapKit。 - Richard Mazkewich
1个回答

0

从图片中我认为这可能是地图可用的最大缩放级别。 如果是这样,屏幕将成为适合所有标记的矩形区域的中心。 尝试将几个标记放在附近并查看边界是否适合您地图上的所有标记,如果成功了,那么恐怕您将无法缩小到此级别以下。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接