如何在iOS中使用CLLocationManager获取当前位置 [英] How to get current location using CLLocationManager in iOS

查看:87
本文介绍了如何在iOS中使用CLLocationManager获取当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经可以使用纬度和经度找到当前位置,但我也希望能够找到给定邮政编码的当前位置。

I can already find the current location using latitude and longitude, but I would also like to be able to find the current location given a zip code.

这是到目前为止我所拥有的:

Here is what I have so far:

.h

#import <MapKit/MapKit.h>
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>


@interface ViewController : UIViewController<CLLocationManagerDelegate>

{

CLLocationManager *locationManager;
 CLLocation *currentLocation;

    IBOutlet UILabel *label1;
    IBOutlet UILabel *lable2;

}
@property (weak, nonatomic) IBOutlet MKMapView *myMapview;
@property (weak, nonatomic) IBOutlet UILabel *label2;
@property (weak, nonatomic) IBOutlet UILabel *lable1;
@end

.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad

{

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    locationManager = [[CLLocationManager alloc]init];



    locationManager.delegate = self;


    locationManager.distanceFilter = kCLDistanceFilterNone;


    locationManager.desiredAccuracy = kCLLocationAccuracyBest;


    [locationManager startUpdatingLocation];


    _myMapview.showsUserLocation = YES;

    [self->locationManager startUpdatingLocation];



    CLLocation *location = [locationManager 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);
    NSLog(@"MY HOME :%@", latitude);
    NSLog(@"MY HOME: %@ ", longitude);

}

#pragma mark CLLocationManager Delegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    currentLocation = [locations objectAtIndex:0];
    [locationManager stopUpdatingLocation];

    [self->locationManager stopUpdatingLocation];




    NSLog(@"my latitude :%f",currentLocation.coordinate.latitude);

    NSLog(@"my longitude :%f",currentLocation.coordinate.longitude);
    label1.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
    lable2.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];


    NSLog(@"Detected Location : %f, %f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
    CLGeocoder *geocoder = [[CLGeocoder alloc] init] ;
    [geocoder reverseGeocodeLocation:currentLocation
                   completionHandler:^(NSArray *placemarks, NSError *error)
     {
                       if (error)
                       {
                           NSLog(@"Geocode failed with error: %@", error);
                           return;
                       }

NSLog(@"Monday");
                       CLPlacemark *placemark = [placemarks objectAtIndex:0];
                       NSLog(@"placemark.ISOcountryCode %@",placemark.ISOcountryCode);

                   }];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil)
    {

    label1.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
    lable2.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
    }
}

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

@end


推荐答案

步骤1: .h 文件中导入 MobileCoreServices 框架

#import< MobileCoreServices / MobileCoreServices.h>

第2步:添加委托 CLLocationManagerDelegate

@interface yourViewController : UIViewController<CLLocationManagerDelegate>
{
    CLLocationManager *locationManager;
    CLLocation *currentLocation;
}

第3步:在类文件中添加此代码

Step 3: Add this code in class file

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self CurrentLocationIdentifier]; // call this method
}

步骤4:方法获取位置

//------------ Current Location Address-----
-(void)CurrentLocationIdentifier
{
    //---- For getting current gps location
    locationManager = [CLLocationManager new];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
    //------
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    currentLocation = [locations objectAtIndex:0];
    [locationManager stopUpdatingLocation];
    CLGeocoder *geocoder = [[CLGeocoder alloc] init] ;
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error)
     {
         if (!(error))
         {
             CLPlacemark *placemark = [placemarks objectAtIndex:0];
            NSLog(@"\nCurrent Location Detected\n");
             NSLog(@"placemark %@",placemark);
             NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
             NSString *Address = [[NSString alloc]initWithString:locatedAt];
             NSString *Area = [[NSString alloc]initWithString:placemark.locality];
             NSString *Country = [[NSString alloc]initWithString:placemark.country];
             NSString *CountryArea = [NSString stringWithFormat:@"%@, %@", Area,Country];
             NSLog(@"%@",CountryArea);
         }
         else
         {
             NSLog(@"Geocode failed with error %@", error);
             NSLog(@"\nCurrent Location Not Detected\n");
             //return;
             CountryArea = NULL;
         }
         /*---- For more results 
         placemark.region);
         placemark.country);
         placemark.locality); 
         placemark.name);
         placemark.ocean);
         placemark.postalCode);
         placemark.subLocality);
         placemark.location);
          ------*/
     }];
}

这篇关于如何在iOS中使用CLLocationManager获取当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆