ios - 当前位置不显示
问题描述
我正在尝试为我的应用程序使用 GoogleMap API 和苹果的 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
任何地方设置该值。
推荐阅读
- c++ - 从列表中删除元素
- jsf - 使用 ui:include 作为递归
- reactjs - 无法读取未定义的属性“会话”[redux-orm][vite]
- azure - 在哪里可以找到 Bot Framework 所需的 ID?
- azure - Azure 负载均衡器:BackendPool 中的虚拟机能否保留公共 IP?
- angular - 无法解构“this.store.selectSnapshot”的属性“IdEvento”
- three.js - threejs 和模板缓冲区用作掩码
- url-rewriting - Apache2 代理后面的 Symfony 5.3 出现意外行为(使用 ProxyPass/ProxyPassReverse 的浏览器中的 url 不正确)
- redis - make[1]: *** [Makefile:349: test] 错误 1
- dropdown - 可搜索的微调器项目未显示?