如何在 iOS 中加载视图时在 Zoom 上显示用户当前位置?

How to Show User Current Location On Zoom When View Did Load in iOS?

我是 iOS 开发的新手,我制作了一个 Goggle 地图支持的应用程序,它显示用户当前位置,但是当我按下我的 GMSMapView 上的 CurrentLocation 按钮时,我会显示用户当前位置,但我想向用户显示当前位置 WithOut Button Press 我的意思是当 viewDidLoad 我用 Zoom 显示直接当前位置请帮助我。

这里我写了一个代码,就像我的 .h File.

#import <UIKit/UIKit.h>
#import <GoogleMaps/GoogleMaps.h>

@interface MapViewController : UIViewController<GMSMapViewDelegate>
@property (nonatomic,retain) IBOutlet GMSMapView *mapView;
@end

以及 .m File like as

的代码
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.mapView.myLocationEnabled = YES;
    self.mapView.mapType = kGMSTypeNormal;
    self.mapView.settings.compassButton = YES;
    self.mapView.settings.myLocationButton = YES;
    self.mapView.delegate = self;
}

请帮助我显示当前位置而不按下当前位置按钮。 请帮助我如何在 Google 地图中获取当前位置地址。

谢谢。

尝试:

[self.mapView animateToLocation: self.mapView.myLocation.coordinate];

请记住,如果位置尚未确定,myLocation 可能是 nil!您可以使用键值观察 (KVO) 来确定何时对当前位置进行动画处理。

-(void) viewDidLoad {
    [super viewDidLoad];
    [self.mapView addObserver:self forKeyPath:@"myLocation" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{

    if ([keyPath isEqualToString:@"myLocation"]) {
        // Animate map to current location (This will run every time user location updates! 
        [self.mapView animateToLocation: self.mapView.myLocation.coordinate];
        // You can remove self from observing 'myLocation' to only animate once
        [self.mapView removeObserver:self forKeyPath:@"myLocation"];
    }
}