无法获取 Google 地图 iOS SDK 的我的位置点
Cannot get the my location dot for Google Maps iOS SDK
目前,我无法在我正在开发的应用程序中获取我的位置。
基本上,在 Google Maps iOS SDK 的文档中,Google 提到您可以:
enable the blue "My Location" dot and compass direction by setting
myLocationEnabled on GMSMapView
https://developers.google.com/maps/documentation/ios/map#my_location
但是,当我这样做时,该点没有出现在我的视图中。
这是我的设置,我有一个包含视图的视图控制器。这个视图包含三个东西,两个按钮和一个地图视图:
我已将我的 mapView 与 GMSMapView 链接起来,我可以毫无问题地查看地图。
现在,我想要的是定位。
我尝试了两件事。首先,使用自定义的草稿按钮(i 表示信息),我尝试手动将位置设置到 mapView,但这没有用,即使 locationServicesEnabled 方法返回 YES。
然后,我尝试使用 GoogleMaps 的点,但这也不起作用。
这是我的代码:
StudyDisplayViewController.h:
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import "GoogleMaps/GoogleMaps.h"
@interface StudyDisplayViewController : UIViewController <CLLocationManagerDelegate> {
}
@property (strong, readwrite) CLLocationManager *locationManager;
@property (weak, nonatomic) IBOutlet GMSMapView *mapView;
@end
StudyDisplayViewController.m
#import "StudyDisplayViewController.h"
@interface StudyDisplayViewController ()
- (IBAction)closeStudyDisplay:(id)sender;
- (IBAction)locateMe:(id)sender;
@end
@implementation StudyDisplayViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
NSLog(@"Starting the location service");
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager startMonitoringSignificantLocationChanges];
if([CLLocationManager locationServicesEnabled]) {
NSLog(@"all good");
}
else {
NSLog(@"Damn son");
}
}
self.mapView.settings.compassButton = YES;
self.mapView.myLocationEnabled = YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
}
-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
NSLog(@"Getting Location");
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (IBAction)closeStudyDisplay:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)locateMe:(id)sender {
NSLog(@"User's location: %@", self.mapView.myLocation);
}
我正在使用 Xcode 6.3.2 和 iOS 8.3。
先看看下面的东西有没有
locationManager = [[CLLocationManager alloc]init];
locationManager.delegate= self;
if([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){
[locationManager requestWhenInUseAuthorization];
}
[locationManager startUpdatingLocation];
_mapView.myLocationEnabled = YES;
_mapView.settings.myLocationButton = YES;
如果一切都在那里,并且如果您正在模拟器上进行测试,则尝试从调试选项卡更改模拟器中的位置,例如将其更改为其中任何一辆免费汽车 运行。
此外,请检查您是否在 PLIST 中为 NSLocationWhenInUseUsageDescription
添加了描述。
目前,我无法在我正在开发的应用程序中获取我的位置。
基本上,在 Google Maps iOS SDK 的文档中,Google 提到您可以:
enable the blue "My Location" dot and compass direction by setting myLocationEnabled on GMSMapView https://developers.google.com/maps/documentation/ios/map#my_location
但是,当我这样做时,该点没有出现在我的视图中。
这是我的设置,我有一个包含视图的视图控制器。这个视图包含三个东西,两个按钮和一个地图视图:
我已将我的 mapView 与 GMSMapView 链接起来,我可以毫无问题地查看地图。
现在,我想要的是定位。
我尝试了两件事。首先,使用自定义的草稿按钮(i 表示信息),我尝试手动将位置设置到 mapView,但这没有用,即使 locationServicesEnabled 方法返回 YES。
然后,我尝试使用 GoogleMaps 的点,但这也不起作用。
这是我的代码:
StudyDisplayViewController.h:
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import "GoogleMaps/GoogleMaps.h"
@interface StudyDisplayViewController : UIViewController <CLLocationManagerDelegate> {
}
@property (strong, readwrite) CLLocationManager *locationManager;
@property (weak, nonatomic) IBOutlet GMSMapView *mapView;
@end
StudyDisplayViewController.m
#import "StudyDisplayViewController.h"
@interface StudyDisplayViewController ()
- (IBAction)closeStudyDisplay:(id)sender;
- (IBAction)locateMe:(id)sender;
@end
@implementation StudyDisplayViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
NSLog(@"Starting the location service");
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager startMonitoringSignificantLocationChanges];
if([CLLocationManager locationServicesEnabled]) {
NSLog(@"all good");
}
else {
NSLog(@"Damn son");
}
}
self.mapView.settings.compassButton = YES;
self.mapView.myLocationEnabled = YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
}
-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
NSLog(@"Getting Location");
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (IBAction)closeStudyDisplay:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)locateMe:(id)sender {
NSLog(@"User's location: %@", self.mapView.myLocation);
}
我正在使用 Xcode 6.3.2 和 iOS 8.3。
先看看下面的东西有没有
locationManager = [[CLLocationManager alloc]init];
locationManager.delegate= self;
if([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){
[locationManager requestWhenInUseAuthorization];
}
[locationManager startUpdatingLocation];
_mapView.myLocationEnabled = YES;
_mapView.settings.myLocationButton = YES;
如果一切都在那里,并且如果您正在模拟器上进行测试,则尝试从调试选项卡更改模拟器中的位置,例如将其更改为其中任何一辆免费汽车 运行。
此外,请检查您是否在 PLIST 中为 NSLocationWhenInUseUsageDescription
添加了描述。