【问题标题】:conversion from objective c to swift3 [closed]从目标 c 转换为 swift3 [关闭]
【发布时间】:2017-09-01 07:03:02
【问题描述】:

在 swift 中使用核心位置获取用户的当前位置

 @implementation MyLocationViewController {
CLLocationManager *locationManager;
CLGeocoder *geocoder;
CLPlacemark *placemark;
}
- (void)viewDidLoad
{
[super viewDidLoad];

    locationManager = [[CLLocationManager alloc] init];
geocoder = [[CLGeocoder alloc] init];
 }

我用来获取位置详细信息的上述代码帮助我使用以下方法转换为 swft

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

if (currentLocation != nil) {
longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}

// Reverse Geocoding
NSLog(@"Resolving the Address");
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
if (error == nil && [placemarks count] > 0) {
placemark = [placemarks lastObject];
addressLabel.text = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@",
placemark.subThoroughfare, placemark.thoroughfare,
placemark.postalCode, placemark.locality,
placemark.administrativeArea,
placemark.country];
} else {
NSLog(@"%@", error.debugDescription);
}
} ];

}

【问题讨论】:

  • 不太清楚你在问什么

标签: swift3 io


【解决方案1】:

试试这个 -

  class MyLocationViewController {
        private var locationManager: CLLocationManager?
        private var geocoder: CLGeocoder?
        private var placemark: CLPlacemark?

        func viewDidLoad() {
            super.viewDidLoad()
            locationManager = CLLocationManager()
            geocoder = CLGeocoder()
        }


    func locationManager(_ manager: CLLocationManager, didUpdateTo newLocation: CLLocation, from oldLocation: CLLocation) {
        print("didUpdateToLocation: \(newLocation)")
        let currentLocation: CLLocation? = newLocation
        if currentLocation != nil {
            longitudeLabel.text = String(format: "%.8f", currentLocation?.coordinate?.longitude)
            latitudeLabel.text = String(format: "%.8f", currentLocation?.coordinate?.latitude)
        }
        // Reverse Geocoding
        print("Resolving the Address")

        geocoder.reverseGeocodeLocation(currentLocation!, completionHandler: {(_ placemarks: [Any], _ error: Error?) -> Void in
            print("Found placemarks: \(placemarks), error: \(error)")
            if error == nil && placemarks.count() > 0 {
                placemark = placemarks.last
                addressLabel.text = "\(placemark.subThoroughfare) \(placemark.thoroughfare)\n\(placemark.postalCode) \(placemark.locality)\n\(placemark.administrativeArea)\n\(placemark.country)"
            }
            else {
                print("\(error?.debugDescription)")
            }
        })
    }
}

【讨论】:

  • 如果可行,也请点赞。
猜你喜欢
  • 2014-06-02
  • 1970-01-01
  • 2012-05-06
  • 2014-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-26
  • 1970-01-01
相关资源
最近更新 更多