如何在谷歌地图iOS中找到当前位置?

Van*_*ian 2 iphone location google-maps ios google-maps-sdk-ios

如何在谷歌地图中显示用户的当前位置?

我必须阅读此表格GPS吗?

Tch*_*low 7

要移至当前位置,您必须:

self.googleMapsView.myLocationEnabled = YES;
Run Code Online (Sandbox Code Playgroud)

然后,您可以在viewWillAppear方法中设置键值观察器,然后使用GoogleMaps SDK的位置管理器获取位置更新.

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    // Implement here to check if already KVO is implemented.
    ...
    [self.googleMapsView addObserver:self forKeyPath:@"myLocation" options:NSKeyValueObservingNew context: nil]
}
Run Code Online (Sandbox Code Playgroud)

然后观察物业.

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"myLocation"] && [object isKindOfClass:[GMSMapView class]])
    {
        [self.googleMapsView animateToCameraPosition:[GMSCameraPosition cameraWithLatitude:self.googleMapsView.myLocation.coordinate.latitude
                                                                                 longitude:self.googleMapsView.myLocation.coordinate.longitude
                                                                                      zoom:self.googleMapsView.projection.zoom]];
    }
}
Run Code Online (Sandbox Code Playgroud)

不要忘记在viewWillDisappear中取消注册观察者.

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    // Implement here if the view has registered KVO
    ...
    [self.googleMapsView removeObserver:self forKeyPath:@"myLocation"];
}
Run Code Online (Sandbox Code Playgroud)

基于Weindl的回答.