Ios 当前位置不显示
我正试图在我的应用程序中使用谷歌地图API和苹果的CLocationManager,但这两个应用程序都没有显示我的当前位置。 我已经在AppDelegate.m中设置了API,并询问和检查用户跟踪位置的权限。这是我的地图代码。 我正在尝试获取用户的当前位置,我将获取用户的目的地,并在用户接近其位置时通知他们(使用估计的时间和距离)。如果你也能帮我,我将不胜感激。谢谢Ios 当前位置不显示,ios,objective-c,cllocationmanager,currentlocation,Ios,Objective C,Cllocationmanager,Currentlocation,我正试图在我的应用程序中使用谷歌地图API和苹果的CLocationManager,但这两个应用程序都没有显示我的当前位置。 我已经在AppDelegate.m中设置了API,并询问和检查用户跟踪位置的权限。这是我的地图代码。 我正在尝试获取用户的当前位置,我将获取用户的目的地,并在用户接近其位置时通知他们(使用估计的时间和距离)。如果你也能帮我,我将不胜感激。谢谢 #import "GMapViewController.h" #import <GoogleMaps/GoogleMaps.
#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
#导入“GMapViewController.h”
#进口
#导入“CSMarker.h”
@导入谷歌地图;
#进口
#进口
@接口GMapViewController()
@属性(强,非原子)nsursession*markerSession;
@属性(强,非原子)GMSPolyline*多段线;
@属性(强,非原子)NSArray*步骤;
//苹果
@属性(弱、非原子)IBMMapView*地图视图;
@结束
@GMapViewController的实现
@综合视图方向、位置管理器;
@综合地图视图;
-(无效)开始和结束更新
{
//如果此对象不存在,请创建位置管理器
//已经有了。
if(nil==位置管理器)
locationManager=[[CLLocationManager alloc]init];
locationManager.delegate=self;
locationManager.desiredAccuracy=KCallocationAccuracyBest;
[locationManager startUpdatingLocation];
}
-(无效)viewDidLoad{
[超级视图下载];
[自启动标准更新];
self.mapView.delegate=self;
self.mapViews.delegate=self;
if([locationManager响应选择器:@selector(RequestWhenUseAuthorization)]){
[locationManager请求始终授权];
}
[locationManager startUpdatingLocation];
//地图类型
self.mapView.mapType=kGMSTypeNormal;
[self.view addSubview:self.mapView];
//显示指南针和我的位置按钮
self.mapView.settings.compassButton=是;
self.mapView.settings.myLocationButton=是;
//设置最大和最小缩放
//[self.mapView setMinZoom:10 maxZoom:18];
//在地图上画一条线
GMSMutablePath*单线路径=[[GMSMutablePath alloc]init];
//创建GMSMutablePath并添加两个点作为lat/lng
[单线路径添加纬度:28.5382经度:-81.3687];
[单线路径添加纬度:28.5421经度:-81.3690];
//使用路径创建GMSPolyline
GMSPolyline*单线=[GMSPolyline polylineWithPath:singleLinePath];
singleLine.map=self.mapView;//打开该行
self.mapViews.showsUserLocation=是;
}
-(无效)未收到记忆警告{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(无效)locationManager:(CLLocationManager*)经理
didFailWithError:(n错误*)错误{
NSString*errorType=nil;
if(error.code==kCLErrorDenied)
{errorType=@“访问决定”;}
其他的
{errorType=@“未知错误”;}
UIAlertController*警报=[UIAlertController]
alertControllerWithTitle:@“警报”
消息:@“获取位置时出错!”
首选样式:UIAlertControllerStyleAlert];
UIAlertAction*okAction=[UIAlertAction
标题为@“确定”的操作
样式:UIAlertActionStyleDefault
处理程序:^(UIAlertAction*操作)
{
NSLog(“正常操作”);
}];
[警报添加操作:确定操作];
[自我呈现视图控制器:警报动画:是完成:无];
}
@结束
再看看您的位置管理器:(CLLocationManager*)管理器更新位置:
方法。您将获得newLocation
引用,但看起来您并没有用它做任何事情。此外,您似乎没有在任何地方设置currentLocation
值。您可以检查您是否在info.plist中定义了以下键吗?这三个键都不是必需的,您需要根据您的使用权限定义其中一个或两个键
NSLocationAlwaysUsageDescription: Always location description
NSLocationWhenInUseUsageDescription: When in use location description
NSLocationAlwaysAndWhenInUseUsageDescription: description for both
将源代码减少到受位置管理器影响的程度会很有帮助。另外,如何设置currentLocation的值?我在h文件中将其设置为CLLocation*currentLocation;但是我不知道如何将它的值设置为实际的当前位置。所以我把它修好了。我的问题是我前面有一层额外的地图,覆盖了我当前的位置。谢谢你的回答。