Getting Longitude/Latitude of the User When the viewDidLoad

后端 未结 5 580
故里飘歌
故里飘歌 2020-12-29 10:22

My purpose is to get the longitude/latitude of the user when the view is loaded and to store the values in two variables for later calculations. I don\'t need to track the u

相关标签:
5条回答
  • 2020-12-29 10:35

    This worked for me

    - (void)viewDidLoad {
        [super viewDidLoad];
        // locationManager update as location
        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self; 
        locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
        locationManager.distanceFilter = kCLDistanceFilterNone; 
        [locationManager startUpdatingLocation];
        [locationManager stopUpdatingLocation];
        CLLocation *location = [locationManager location];
        // Configure the new event with information from the location
    
        float longitude=location.coordinate.longitude;
        float latitude=location.coordinate.latitude;
    
        NSLog(@"dLongitude : %f", longitude);
        NSLog(@"dLatitude : %f", latitude); 
    }
    
    0 讨论(0)
  • 2020-12-29 10:40

    As soon as you call [locationManager startUpdatingLocation]; your viewDidLoad method is done. You need to implement

    -(void) locationManager: (CLLocationManager *)manager didUpdateToLocation: (CLLocation *) newLocation 
               fromLocation: (CLLocation *) oldLocation
    

    and

    - (void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
    

    which will be called by your locationManager when it gets the location, or when it fails. In both of those methods, you can call [locationManager stopUpdatingLocation].

    Inside the didUpdateToLocation method, you should move all your code from viewDidLoad, starting with CLLocation *location = [locationManager location]; That's where you'll get proper values.

    0 讨论(0)
  • 2020-12-29 10:48

    To get user's location even once you need to make locationManager to start updating locations (you did that) and implement delegate method that manager calls when location is retrieved - you can't get location from location manager immediately.

    If you don't want to track user location - just stop updating locations in that delegate method after location was fetched for the 1st time (and store it if you need it in future).

    0 讨论(0)
  • 2020-12-29 10:50

    Try this Code

    -(IBAction)Button:(id)sender
    
    {
    
    locationManager = [[CLLocationManager alloc] init]; 
    
    locationManager.delegate = self;
    
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    
    locationManager.distanceFilter = kCLDistanceFilterNone;
    
    [locationManager startUpdatingLocation];
    
    CLLocation *location = [locationManager location];
    

    // Configure the new event with information from the location

    CLLocationCoordinate2D coordinate = [location coordinate];
    
    NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude];
    
    NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];
    
    NSLog(@"dLatitude : %@", latitude);
    
    NSLog(@"dLongitude : %@",longitude);
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 40, 250, 50)];
    
    UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 80, 200, 50)];
    
    UILabel *myLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(50, 120, 200, 100)];
    
    myLabel.textColor = [UIColor blackColor];
    
    myLabel1.textColor = [UIColor blackColor];
    
    label.backgroundColor = [UIColor clearColor];
    
    myLabel.backgroundColor = [UIColor clearColor];
    
    myLabel1.backgroundColor = [UIColor clearColor];
    
    [myLabel setText:latitude];
    
    [myLabel1 setText:longitude];
    
    label.text = @"Current Latitude And Longitude";
    
    [self.view addSubview:label];
    
    [self.view addSubview:myLabel];
    
    [self.view addSubview:myLabel1];
    
    }
    
    0 讨论(0)
  • 2020-12-29 11:00

    if you are getting Lat lang both (0.00 ,0.00)

    this may be solution for your ,

    step 1: NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription

    Add these two key in Info.plist

    step 2: And than [locationManager startUpdatingLocation]; start updating

    step 3: and you need to implement two mehthods

    -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    
    }
    
    -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    
    
    
     CLLocation * currentLocation = [locations lastObject];
    
        NSLog(@"Updated Lat %f",currentLocation.coordinate.longitude);
        NSLog(@"Updated Lang %f",currentLocation.coordinate.latitude);
    
    
    
    }
    

    of CLLocationManagerDelegate in your ViewController interface.

    **step 4:**
    
    -(void)viewDidLoad
    {
        [super viewDidLoad];
     geocoder = [[CLGeocoder alloc] init];
        manager.delegate = self;
        manager.desiredAccuracy = kCLLocationAccuracyBest;
        [manager requestWhenInUseAuthorization];
        [manager requestAlwaysAuthorization];
        [manager startUpdatingLocation];
    
    
    }
    
    0 讨论(0)
提交回复
热议问题