iOS 定位当前城市

来源:互联网 发布:起名字软件 编辑:程序博客网 时间:2024/06/10 02:28
引入框架:CoreLocation

.h文件

引入CoreLocation/CoreLocation.h

@interface WeatherViewController :UIViewController<</span>CLLocationManagerDelegate>{

    CLLocationManager* locationManager;

}


@property (strongnonatomicCLLocationManager* locationManager;


@end



.m文件

//开始定位

-(void)startLocation{

    self.locationManager = [[CLLocationManager alloc] init];

    self.locationManager.delegate = self;

    self.locationManager.desiredAccuracy =kCLLocationAccuracyBest;

    self.locationManager.distanceFilter = 10.0f;

    [self.locationManager startUpdatingLocation];

}


//定位代理经纬度回调

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

    

    [locationManager stopUpdatingLocation];

    NSLog(@"location ok");


    NSLog(@"%@",[NSString stringWithFormat:@"经度:%3.5f\n纬度:%3.5f",newLocation.coordinate.latitude,newLocation.coordinate.longitude]);

    

    CLGeocoder * geoCoder = [[CLGeocoder allocinit];

    [geoCoder reverseGeocodeLocation:newLocationcompletionHandler:^(NSArray *placemarks, NSError *error) {

        for (CLPlacemark * placemark in placemarks) {

            

            NSDictionary *test = [placemark addressDictionary];

            //  Country(国家 State(城市 SubLocality()

            NSLog(@"%@", [test objectForKey:@"State"]);

        }

    }];

 

}

0 0
原创粉丝点击