2014-12-07 102 views
0

我有一個導入SWRevealViewController的MapViewController,我老實說沒有在下面編寫這行代碼。我試圖運行該應用程序,但不幸的是地圖沒有顯示當前位置,即使該應用程序被授予訪問GPS的權限,即使位置服務也無法打開手機。由於我不是iOS開發人員的期望,所以我需要有人來查看代碼並解釋此代碼中缺少的內容。我研究了類似的問題,但即使對於學習Objective-C的人來說,他們也顯得很直接。我認爲這個代碼的問題是在這裏導入的SWRevealViewController,我認爲這是一個庫或我不知道的東西。iOS地圖標記和當前位置沒有顯示

#import "MapViewController.h" 
 
#import "SWRevealViewController.h" 
 

 
// New York 
 
#define NY_LATITUDE 40.754445 
 
#define NY_LONGITUDE -73.977364 
 

 
// Span and distance 
 
#define SPAN_VALUE 1.0f 
 
#define DISTANCE 500 
 

 

 

 
@interface MapViewController() 
 
- (void)startLocationServices; 
 
- (void)stopLocationServices; 
 
@end 
 

 
@implementation MapViewController 
 

 
@synthesize locationManager; 
 
@synthesize gpsDisplay; 
 
@synthesize mapk; 
 

 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
 
{ 
 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
 
    if (self) { 
 
     // Custom initialization 
 
    } 
 
    return self; 
 
} 
 
- (IBAction)sendRequest:(id)sender { 
 
     [self alertStatus:@"An assistance request has been sent, you will be contacted shortly." :@"Send assistance request" :0]; 
 
    [self performSegueWithIdentifier:@"sendRequest" sender:nil]; 
 
    
 
} 
 

 
- (void) alertStatus:(NSString *)msg :(NSString *)title :(int) tag 
 
{ 
 
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title 
 
                 message:msg 
 
                 delegate:self 
 
               cancelButtonTitle:@"Ok" 
 
               otherButtonTitles:nil, nil]; 
 
    alertView.tag = tag; 
 
    [alertView show]; 
 
} 
 
-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex: (NSInteger)buttonIndex 
 
{ 
 
    if(buttonIndex==0) 
 
    { 
 
     //NSLog(@"OK Clicked"); 
 
     // [self alertStatus:@"An assistance request has been sent, you will be contacted shortly. " :@"I just had an accident" :0]; 
 
     // [self performSegueWithIdentifier:@"checkProgress" sender:nil]; 
 
     
 
     
 
    }} 
 

 
- (void)viewDidLoad 
 
{ 
 
    [super viewDidLoad]; 
 
    
 
    [self startLocationServices]; 
 
    
 
    if ([CLLocationManager locationServicesEnabled]) { 
 
     self.locationManager = [[CLLocationManager alloc] init]; 
 
     self.locationManager.delegate = self; 
 
     [self.locationManager startUpdatingLocation]; 
 
    } else { 
 
     NSLog(@"Location services are not enabled"); 
 
    } 
 
    
 
    // Set a timer to stop the Location services 
 
    //[NSTimer scheduledTimerWithTimeInterval:100.0 target:self selector:@selector(stopLocationServices) userInfo:nil repeats:NO]; 
 
    
 
    _sidebarButton.tintColor = [UIColor colorWithWhite:0.1f alpha:0.9f]; 
 
    
 
    // Set the side bar button action. When it's tapped, it'll show up the sidebar. 
 
    _sidebarButton.target = self.revealViewController; 
 
    _sidebarButton.action = @selector(revealToggle:); 
 
    
 
    // Set the gesture 
 
    [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer]; 
 
// CLLocationManager *cllocationManager = [[CLLocationManager alloc] init]; 
 
// cllocationManager.delegate = self; 
 
// cllocationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; 
 
// cllocationManager.distanceFilter = 20.0f; 
 
//  
 
// if ([CLLocationManager locationServicesEnabled]) 
 
// { 
 
//  [cllocationManager startUpdatingLocation]; 
 
// } 
 
// else 
 
// { 
 
//  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Turn On Location Services to find your location" 
 
//              message:nil delegate:nil 
 
//            cancelButtonTitle:@"OK" 
 
//            otherButtonTitles:nil]; 
 
//  [alert show]; 
 
//  // [alert release]; 
 
// } 
 
} 
 

 

 

 

 
- (IBAction)mapChanger:(id)sender { 
 
    
 
    switch (((UISegmentedControl *) sender).selectedSegmentIndex) { 
 
     case 0: 
 
      self.mapk.mapType = MKMapTypeStandard; 
 
      break; 
 
     case 1: 
 
      self.mapk.mapType = MKMapTypeSatellite; 
 
      break; 
 
     case 2: 
 
      self.mapk.mapType = MKMapTypeHybrid; 
 
      break; 
 
      
 
     default: 
 
      break; 
 
    } 
 
} 
 

 
- (void)startLocationServices 
 
{ 
 
    // create the Location Manager 
 
    if (self.locationManager == nil) { 
 
     self.locationManager = [CLLocationManager new]; 
 
    } 
 
    
 
    // settings 
 
    [self.locationManager setDelegate:self]; 
 
    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; 
 
    [self.locationManager setDistanceFilter:kCLDistanceFilterNone]; 
 
    
 
    // start services 
 
    [self.locationManager startUpdatingLocation]; 
 
    self.gpsDisplay.text = @"Location Service started."; 
 
} 
 

 
- (void)stopLocationServices 
 
{ 
 
    // stop services 
 
    [self.locationManager stopUpdatingLocation]; 
 
    [self.locationManager setDelegate:nil]; 
 
    self.gpsDisplay.text = @"Location Services stopped."; 
 
} 
 

 
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation 
 
{ 
 
    CLLocationCoordinate2D loc = [userLocation coordinate]; 
 
    // CLLocationDistance in meters (1 meter = 3.3 feet) 
 
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, DISTANCE, DISTANCE); 
 
    [mapView setRegion:region animated:YES]; 
 
} 
 

 
- (void)locationManager:(CLLocationManager *)manager 
 
    didUpdateToLocation:(CLLocation *)newLocation 
 
      fromLocation:(CLLocation *)oldLocation { 
 
    NSString *coords [email protected]"lat"; 
 
    coords = [coords stringByAppendingString:[NSString stringWithFormat:@"%f", newLocation.coordinate.latitude]]; 
 
    coords = [coords stringByAppendingString:@"lon"]; 
 
    coords = [coords stringByAppendingString:[NSString stringWithFormat:@"%f", newLocation.coordinate.longitude]]; 
 
    
 
    self.gpsDisplay.text = coords; 
 
    
 
    MKCoordinateRegion region; 
 
    region.center.latitude = newLocation.coordinate.latitude; 
 
    region.center.longitude = newLocation.coordinate.longitude; 
 
    region.span.latitudeDelta = SPAN_VALUE; 
 
    region.span.longitudeDelta = SPAN_VALUE; 
 
    [self.mapk setRegion:region animated:YES]; 
 
    
 
} 
 

 
- (void)viewDidUnload { 
 
    //[self setUpdates:nil]; 
 
    [super viewDidUnload]; 
 
} 
 

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

回答

0

在iOS 8,他們改變了你問位置的權限。您只需致電requestWhenInUseAuthorizationrequestAlwaysAuthorization即可,而不必致電startUpdatingLocation

+0

也許因爲這個變化,這就是爲什麼代碼不再運行。我會嘗試你的建議。謝謝 – 2014-12-08 06:33:47

1

除了前面的回答,請注意,您必須指定用於訪問用戶的位置信息將相對調(NSLocationAlwaysUsageDescription或NSLocationWhenInUseUsageDescription)到項目的Info.plist

例如原因:

<key>NSLocationWhenInUseUsageDescription</key> 
<string>Location is required to find out jobs around you are</string> 
+0

感謝你的朋友你的解釋是好的。 – 2014-12-08 06:31:36

相關問題