从目标c转换为swift3
使用swift中的核心位置获取用户的当前位置从目标c转换为swift3,swift3,io,Swift3,Io,使用swift中的核心位置获取用户的当前位置 @implementation MyLocationViewController { CLLocationManager *locationManager; CLGeocoder *geocoder; CLPlacemark *placemark; } - (void)viewDidLoad { [super viewDidLoad]; locationManager = [[CLLocationManager alloc] init];
@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);
}
} ];
}
试试这个-
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)")
}
})
}
}
不是很清楚你在问什么,如果有效的话也请投票。