gpt4 book ai didi

ios - 获取用户的当前位置 iOS 8.0

转载 作者:行者123 更新时间:2023-11-29 01:15:04 25 4
gpt4 key购买 nike

我尝试使用 MapKit 和 CoreLocation 获取用户当前位置。我真的是 Objective-C 的新手。至于我的研究,从旧版 iOS 到 iOS 8.0 的实现略有不同。我已经正确地遵循了一切。它仍然在获取当前位置。我的实际目标是当用户在文本字段中提供位置时,在运行时在 mapKit 上获取固定用户位置。

filename.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface LocationSelectionViewController : UIViewController <CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet UITextField *insertLocation;
@property (strong, nonatomic) IBOutlet MKMapView *serviceLocation;
- (IBAction)next:(id)sender;

@end

#import "LocationSelectionViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface LocationSelectionViewController () <CLLocationManagerDelegate>

@end

@implementation LocationSelectionViewController
CLLocationManager *locationManager;
@synthesize serviceLocation;

- (void)viewDidLoad {
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager requestAlwaysAuthorization];
[locationManager startUpdatingLocation];
// Do any additional setup after loading the view.
self.serviceLocation = [[MKMapView alloc] initWithFrame:self.view.bounds];
self.serviceLocation.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.serviceLocation];
[self.serviceLocation.delegate self];
//[self.serviceLocation setShowsUserLocation:YES];
}

/*- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
CLLocationCoordinate2D loc=[userLocation coordinate];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 100, 100);
[self.serviceLocation setRegion:region animated:YES];
}*/

-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
[locationManager requestAlwaysAuthorization];
[locationManager startUpdatingLocation];

}
-(void) locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
if(status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse){
self.serviceLocation.showsUserLocation = YES;
}
}

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

/*
#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)next:(id)sender {
}
@end

如图所示,我已根据 iOS 8 要求添加了两个属性。

image

谁能建议我存档:

  • 获取用户当前位置。
  • 在运行时从文本字段获取位置并固定在 MapKip 上。

最佳答案

您请求和启动位置更新的流程不正确。首先,requestAlwaysAuthorization 不会立即更改授权状态,因为它需要用户在警报中按下 AllowDon't Allow 按钮。所以你应该在得到结果后才开始位置更新。然后在didUpdateLocations 不需要重新请求/开始。

按照下面的方式做,

Dn 你的 viewDidLoad

 BOOL isAuthorized = [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways;
if (isAuthorized) {
[locationManager startUpdatingLocation];
}
else {
[locationManager requestAlwaysAuthorization];
}

然后像这样实现委托(delegate)方法

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
CLLocation *current = locations.firstObject;
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
if(status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse){
self.serviceLocation.showsUserLocation = YES;
[locationManager startUpdatingLocation];
}
}

在 iOS 8 中,为了请求权限,您应该在 info.plist 中添加用法描述字符串。在您的情况下,关键是 NSLocationAlwaysUsageDescription

关于ios - 获取用户的当前位置 iOS 8.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35284909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com