当前位置不显示

时间:2018-04-30 22:06:53

标签: ios objective-c cllocationmanager currentlocation

我正在尝试将GoogleMap API和Apple的CLocationManager用于我的应用,但它并未在其中任何一个中显示我当前的位置。 我在AppDelegate.m中设置了API,并询问并检查用户跟踪位置的权限。这是我对地图的代码。 我正在尝试获取用户的当前位置,我将获得用户的目的地,并在他们靠近他们的位置时通知他们(使用估计的时间和距离)。如果你能帮助我,我将不胜感激。感谢

#import "GMapViewController.h"
#import <GoogleMaps/GoogleMaps.h>
#import "CSMarker.h"
@import GoogleMaps;
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>


@interface GMapViewController ()  <GMSMapViewDelegate, CLLocationManagerDelegate, MKMapViewDelegate>
@property(strong, nonatomic) NSURLSession *markerSession;
@property(strong, nonatomic) GMSPolyline *polyline;
@property(strong, nonatomic) NSArray *steps;

//apple 

@property (weak, nonatomic) IBOutlet MKMapView *mapViews;

@end


@implementation GMapViewController
@synthesize viewDirection,locationManager;
@synthesize mapView;

- (void)startStandardUpdates
{
    // Create the location manager if this object does not
    // already have one.
    if (nil == locationManager)
        locationManager = [[CLLocationManager alloc] init];

    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self startStandardUpdates];
    self.mapView.delegate=self;
    self.mapViews.delegate=self;

    if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [locationManager requestAlwaysAuthorization];
    }
    [locationManager startUpdatingLocation];



    //map type
    self.mapView.mapType = kGMSTypeNormal;
    [self.view addSubview:self.mapView];
    // to show compass and mylocation button
    self.mapView.settings.compassButton = YES;
    self.mapView.settings.myLocationButton = YES;
    //setting max and min zoom
    //[self.mapView setMinZoom:10 maxZoom:18];



    //for Drawing a line on the map
    GMSMutablePath *singleLinePath = [[GMSMutablePath alloc] init];
    // create a GMSMutablePath and add two points as lat/lng
    [singleLinePath addLatitude:28.5382 longitude:-81.3687];
    [singleLinePath addLatitude:28.5421 longitude:-81.3690];
    // use the path to create a GMSPolyline
    GMSPolyline *singleLine = [GMSPolyline polylineWithPath:singleLinePath];
    singleLine.map = self.mapView; //turn the line on

        self.mapViews.showsUserLocation = YES;
} 


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error {
    NSString *errorType = nil;
    if (error.code == kCLErrorDenied)
    {errorType = @"Access Decied";}
    else
    {errorType = @"Unknown Error";}
    UIAlertController* alert = [UIAlertController
                                alertControllerWithTitle: @"Alert"
                                message: @"Error getting location!"
                                preferredStyle: UIAlertControllerStyleAlert];


    UIAlertAction *okAction = [UIAlertAction
                               actionWithTitle:@"OK"
                               style:UIAlertActionStyleDefault
                               handler:^(UIAlertAction *action)
                               {
                                   NSLog(@"OK action");

                               }];

    [alert addAction:okAction];
    [self presentViewController:alert animated:YES completion:nil];

}

@end

2 个答案:

答案 0 :(得分:0)

再看一下您的locationManager:(CLLocationManager *)manager didUpdateLocations:方法。您正在获取newLocation引用,但它看起来不像您使用它做任何事情。此外,您似乎没有在任何地方设置currentLocation值。

答案 1 :(得分:0)

您可以检查一下info.plist中是否定义了以下键吗?不需要所有三个键,您需要根据您的使用权限定义其中一个或两个键。

NSLocationAlwaysUsageDescription: Always location description
NSLocationWhenInUseUsageDescription: When in use location description
NSLocationAlwaysAndWhenInUseUsageDescription: description for both
相关问题