GMSMapView myLocation没有给出实际的位置 [英] GMSMapView myLocation not giving actual location

查看:191
本文介绍了GMSMapView myLocation没有给出实际的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a GMSMapView properly loaded and working inside my viewcontroller

我无法做的就是将GMSCameraPosition设置在我的位置。

what i'm not being able to do is setting the GMSCameraPosition around my location

这是我的代码:

this is my code:

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返回零CLLocation,因此 cameraWithLatitude:longitude:zoom:获得0 0坐标并显示非洲而不是我的实际位置(非非洲:))

GPS is enabled and application has all needed permissions but myLocation returns a nil CLLocation, consequentially cameraWithLatitude:longitude:zoom: get 0 0 coordinates and displays Africa instead of my actual location (that is not in africa :) )

推荐答案

从官方的Google Maps iOS SDK文档:
$ b

From official Google Maps iOS SDK documentation:



    < )myLocationEnabled [读取,写入,分配]
    控制是否启用我的位置点和精确度圆圈。
  • (BOOL) myLocationEnabled [read, write, assign] Controls whether the My Location dot and accuracy circle is enabled.

默认为NO。


  • (CLLocation *)myLocation [读取,分配]
    如果启用了我的位置,

  • (CLLocation*) myLocation [read, assign] If My Location is enabled, reveals where the user location dot is being drawn.

如果它被禁用或已启用,但没有位置数据可用,则这将为零。

If it is disabled, or it is enabled but no location data is available, this will be nil. This property is observable using KVO.

所以当你设置 mapView_.myLocationEnabled = YES; ,它只会告诉 mapView 只有在您给 myLocation 属性。来自Google的示例代码展示了如何使用KVO方法观察用户位置(推荐)您还可以实现 CLLocationManagerDelegate 方法来更新mapView。

So when you set mapView_.myLocationEnabled = YES;, it only tells the mapView to reveal the blue dot only if you have a location value given to the myLocation property. The sample code from Google shows how to observe the user location using the KVO method.(Recommended) You can also implement the CLLocationManagerDelegate method, to update the mapView.

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

下面是代码谷歌地图示例代码如何使用KVO更新用户位置。

Here are the code from google maps sample code on how to use KVO to update user location.

// 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];
  }
}

这篇关于GMSMapView myLocation没有给出实际的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆