当前位置不显示
Current location does not show
我正在尝试为我的应用程序使用 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
再看看你的 locationManager:(CLLocationManager *)manager didUpdateLocations:
方法。您正在获取 newLocation
引用,但您似乎没有对其进行任何操作。此外,您似乎没有在任何地方设置 currentLocation
值。
你能检查一下你是否在你的 info.plist 中定义了以下键吗?不需要所有三个键,您需要根据您的使用权限定义其中一个或两个键。
NSLocationAlwaysUsageDescription: Always location description
NSLocationWhenInUseUsageDescription: When in use location description
NSLocationAlwaysAndWhenInUseUsageDescription: description for both
我正在尝试为我的应用程序使用 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
再看看你的 locationManager:(CLLocationManager *)manager didUpdateLocations:
方法。您正在获取 newLocation
引用,但您似乎没有对其进行任何操作。此外,您似乎没有在任何地方设置 currentLocation
值。
你能检查一下你是否在你的 info.plist 中定义了以下键吗?不需要所有三个键,您需要根据您的使用权限定义其中一个或两个键。
NSLocationAlwaysUsageDescription: Always location description
NSLocationWhenInUseUsageDescription: When in use location description
NSLocationAlwaysAndWhenInUseUsageDescription: description for both