2016-02-09 37 views
0

我試圖讓用戶在使用MapKit和CoreLocation當前位置。我對Objective-C非常陌生。至於我的研究,實現與舊版iOS到iOS 8.0稍有不同。我一切正確。它仍然在獲取當前位置。我的實際目標是在用戶在文本字段中提供位置時,在運行時獲取pinkit用戶位置。獲取用戶當前位置的iOS 8.0

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上。
+0

僅供參考 - 你的'locationManager'變量是一個文件全局變量。這是你真正想要的(可能不是)? – rmaddy

+0

1.獲取用戶當前位置並放置一個pin: - http://stackoverflow.com/questions/23922625/drop-pin-on-current-location 2.從文本字段獲取位置並鎖定 - http:// stackoverflow.com/questions/7459801/mkmapview-place-pin-at-location-long-lat – Vizllx

回答

1

您請求和開始位置更新的流程不正確。首先requestAlwaysAuthorization不會立即更改授權狀態,因爲它需要用戶按下警報中的AllowDon't Allow按鈕。所以你只有在得到結果後才能開始位置更新。然後在didUpdateLocations不需要再次請求/重新啓動。

做在下面的方式,

DN您viewDidLoad

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

而且實現委託方法,如

- (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