ios – 缩放GMSMapView以适应GMSMarker标记?

前端之家收集整理的这篇文章主要介绍了ios – 缩放GMSMapView以适应GMSMarker标记?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我期待着这个代码,他会将所有标记放在地图上并缩放GMSMapView以适应我所有的GMSMarker,我用它来包括GMSCoordinateBounds类的Coordinate方法,但它不能按我的意愿工作.

我的例子:

#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

这是此代码的结果:

enter image description here

解决方法

从图像我认为这可能是地图可用的最大缩小级别. 如果这样,屏幕将成为适合所有标记的矩形区域的中心. 尝试将几个标记靠近并看到边界适合地图中的所有标记,如果你成功,那么我恐怕你不会超出这个水平.

猜你在找的iOS相关文章