博客> #pragma mark - 定位代理方法 ()
#pragma mark - 定位代理方法 ()
2019-10-18 22:32 评论:0 阅读:454 魔灵
  • (void)locationManager:(CLLocationManager )manager didUpdateToLocation:(CLLocation )newLocation fromLocation:(CLLocation )oldLocation { //将经度显示到label上 //self.longitude.text = [NSString stringWithFormat:@"%lf", newLocation.coordinate.longitude]; self.longitude = [NSString stringWithFormat:@"%f", newLocation.coordinate.longitude]; //将纬度现实到label上 // self.latitude.text = [NSString stringWithFormat:@"%lf", newLocation.coordinate.latitude]; self.latitude = [NSString stringWithFormat:@"%f", newLocation.coordinate.latitude]; // 获取当前所在的城市名 CLGeocoder geocoder = [[CLGeocoder alloc] init]; //根据经纬度 反向地理编译出地址信息 [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray array, NSError error) {

                   if (array.count > 0) {
                       CLPlacemark *placemark = [array objectAtIndex:0];
    
                       self.location = placemark.name;
                       //                           NSLog(@"当前地址是:%@", self.location);
                       //获取城市
                       NSString *city = placemark.locality;
                       if (!city) {
                           //四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
                           city = placemark.administrativeArea;
                       }
                       //                           NSLog(@"city = %@", city);
                       //            _cityLable.text = city;
                       //            [_cityButton setTitle:city forState:UIControlStateNormal];
                   }
                   else if (error == nil && [array count] == 0) {
                       //                           NSLog(@"No results were returned.");
                   }
                   else if (error != nil) {
                       //                           NSLog(@"An error occurred = %@", error);
                   }
    
               }];

    //系统会一直更新数据,直到选择停止更新,因为我们只需要获得一次经纬度即可,所以获取之后就停止更新 [manager stopUpdatingLocation]; }

收藏
0
sina weixin mail 回到顶部