【问题标题】:Unable to get current location coordinates using Google Maps API for iOS无法使用适用于 iOS 的 Google Maps API 获取当前位置坐标
【发布时间】:2014-06-09 01:09:50
【问题描述】:

我试过下面的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    mapView_.myLocationEnabled = YES;

    CLLocation *myLocation = mapView_.myLocation;
    NSLog(@"%f %f",myLocation.coordinate.latitude, myLocation.coordinate.longitude);
}

记录的输出是 0.000000 0.000000。

我还检查了位置服务已启用并且也显示为我的应用启用的设置。

请帮我弄清楚我哪里出错了。

【问题讨论】:

  • 您是在 XCode 还是应用程序上进行调试?如果是 XCode,您确定您在底栏的 XCode 中打开了模拟位置并将其设置为实际位置吗?如果这没问题,那么看起来您并没有使用 CLLocationManager。
  • 非常感谢。有效! :)

标签: ios google-maps ios7 google-maps-sdk-ios


【解决方案1】:

viewDidLoad中总是显示为0, 0,你必须使用KVO来观察坐标。试试下面的代码:-

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.mapView addObserver:self forKeyPath:@"myLocation" options:NSKeyValueObservingOptionNew context: nil];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.mapView removeObserver:self forKeyPath:@"myLocation"];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"myLocation"] && [object isKindOfClass:[GMSMapView class]])
    {
        NSLog(@"KVO triggered. Location Latitude: %f Longitude: %f",self.mapView.myLocation.coordinate.latitude,self.mapView.myLocation.coordinate.longitude);
    }
}

【讨论】:

    【解决方案2】:

    设备和应用程序需要一些时间才能确定您的位置。它在启动时不可用。您将需要监听位置的变化。

    此答案描述了如何在 myLocation 更改时收到通知:

    https://stackoverflow.com/a/15291319/148241

    【讨论】:

      猜你喜欢
      • 2013-06-08
      • 1970-01-01
      • 2014-01-24
      • 1970-01-01
      • 2016-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-14
      相关资源
      最近更新 更多