【问题标题】:GMSMapView myLocation not giving actual locationGMSMapView myLocation 未提供实际位置
【发布时间】:2013-06-28 13:57:38
【问题描述】:

我有一个 GMSMapView 正确加载并在我的视图控制器中工作

我无法做的是在我的位置周围设置 GMSCameraPosition

这是我的代码:

mapView_.myLocationEnabled = YES;
CLLocation* myLoc = [mapView_ myLocation];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:myLoc.coordinate.latitude
                                                        longitude:myLoc.coordinate.longitude
                                                             zoom:4];
[mapView_ setCamera:camera];

GPS 已启用并且应用程序具有所有需要的权限,但 myLocation 返回 nil CLLocation,因此 cameraWithLatitude:longitude:zoom: 获取 0 0 坐标并显示非洲而不是我的实际位置(不在非洲 :))

【问题讨论】:

    标签: ios objective-c google-maps google-maps-sdk-ios


    【解决方案1】:

    来自官方 Google Maps iOS SDK 文档:

    • (BOOL) myLocationEnabled [读取、写入、分配] 控制是否启用“我的位置”圆点和精确圆圈。

    默认为 NO。

    • (CLLocation*) myLocation [读取,分配] 如果启用了“我的位置”,则会显示绘制用户位置点的位置。

    如果它被禁用,或者它被启用但没有可用的位置数据,这将为 nil。使用 KVO 可以观察到此属性。

    因此,当您设置mapView_.myLocationEnabled = YES; 时,它只会告诉mapView 仅在您为myLocation 属性提供位置值时才显示蓝点。来自 Google 的示例代码展示了如何使用 KVO 方法观察用户位置。(推荐)您还可以实现 CLLocationManagerDelegate 方法来更新 mapView。

    -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    {
        [mapView animateToLocation:newLocation.coordinate];
        // some code...
    }
    

    以下是谷歌地图示例代码中关于如何使用 KVO 更新用户位置的代码。

    // in viewDidLoad method...
    // Listen to the myLocation property of GMSMapView.
      [mapView_ addObserver:self
                 forKeyPath:@"myLocation"
                    options:NSKeyValueObservingOptionNew
                    context:NULL];
    // Ask for My Location data after the map has already been added to the UI.
      dispatch_async(dispatch_get_main_queue(), ^{
        mapView_.myLocationEnabled = YES;
      });
    
    - (void)observeValueForKeyPath:(NSString *)keyPath
                          ofObject:(id)object
                            change:(NSDictionary *)change
                           context:(void *)context {
      if (!firstLocationUpdate_) {
        // If the first location update has not yet been recieved, then jump to that
        // location.
        firstLocationUpdate_ = YES;
        CLLocation *location = [change objectForKey:NSKeyValueChangeNewKey];
        mapView_.camera = [GMSCameraPosition cameraWithTarget:location.coordinate
                                                         zoom:14];
      }
    }
    

    【讨论】:

    • 你的 firstLocationUpdate_ 变量只是一个布尔属性吗?
    • @kevinl 是的,实际上,第二个代码 sn-ps 是从 google 示例代码中复制的...
    • 不敢相信 Google 没有在 GMSMapViewDelegate 中包含方法,在这种情况下使用 KVO 有点黑客行为......
    【解决方案2】:

    尝试使用 MapKit,在 VieDidLoadViewWillApper 下使用:

    myMapView.showsUserLocation = YES;
    

    如果需要,您可以在 IBAction 下添加此简单代码,例如:

    - (IBAction)getLocation:(id)sender{
    
             myMapView.showsUserLocation = YES;
    
    }
    

    希望对你有帮助

    【讨论】:

    • 我需要使用谷歌地图,而且 MapKit 和 GMS 以任何方式协同工作
    • 但 mapKit 使用谷歌地图,我认为但不确定是否具有与“showUserLocation”或“getUserLocation”相同的方法,那么您可以添加缩放和“coordinate.latitude”和“longitude”跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多