GMSMapView myLocation not giving actual location

≯℡__Kan透↙ 提交于 2019-11-29 19:59:34

问题


I have a GMSMapView properly loaded and working inside my viewcontroller

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 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 :) )


回答1:


From official Google Maps iOS SDK documentation:

  • (BOOL) myLocationEnabled [read, write, assign] Controls whether the My Location dot and accuracy circle is enabled.

Defaults to NO.

  • (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.

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...
}

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



回答2:


Try to using MapKit, under the VieDidLoad or ViewWillApper using:

myMapView.showsUserLocation = YES;

You can add this simple code under the IBAction if you want, like a :

- (IBAction)getLocation:(id)sender{

         myMapView.showsUserLocation = YES;

}

I hope this help you



来源:https://stackoverflow.com/questions/17366403/gmsmapview-mylocation-not-giving-actual-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!