managerLocation在swift中不起作用

9 浏览
0 Comments

managerLocation在swift中不起作用

我的代码在`viewDidLoad`函数中是这样的:

manager = CLLocationManager()

manager.delegate = self

manager.desiredAccuracy = kCLLocationAccuracyBest

manager.requestWhenInUseAuthorization()

manager.startUpdatingLocation()

我定义了`locationManager`函数如下:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

let mylocation:CLLocation = manager.location!

let mylatatiude:CLLocationDegrees = mylocation.coordinate.latitude

let mylongitude:CLLocationDegrees = mylocation.coordinate.longitude

let mydetlalatatude:CLLocationDegrees = 0.01

let mydetlongitude:CLLocationDegrees = 0.01

let myspan:MKCoordinateSpan = MKCoordinateSpanMake(mydetlalatatude, mydetlongitude)

let mycurrentlocation :CLLocationCoordinate2D = CLLocationCoordinate2DMake(mylatatiude, mylongitude)

let myregion:MKCoordinateRegion = MKCoordinateRegionMake(mycurrentlocation, myspan)

mapview.setRegion(myregion, animated: true)

manager.stopUpdatingLocation()

}

然后,我会像这样标记我的位置:

let WonderAnnotation = MKPointAnnotation()

WonderAnnotation.coordinate = mycurrentlocation

WonderAnnotation.title="here you are"

mapview.addAnnotation(WonderAnnotation)

我想显示我的地址:

CLGeocoder().reverseGeocodeLocation(mylocation, completionHandler: { (placemarks, error) in

if(error != nil){

print("error: \(error)")

} else {

let p = CLPlacemark(placemark: (placemarks?[0] as CLPlacemark!))

var subthoroughFare:String = ""

var thoroughfrae:String=""

var sublocality:String=""

var subadministrativearea:String=""

var postalcode:String=""

var country:String = ""

if ((p.subThoroughfare) != nil){

subthoroughFare = (p.subThoroughfare)!

}

if (p.thoroughfare != nil){

thoroughfrae = p.thoroughfare!

}

if (p.subLocality != nil){

sublocality = p.subLocality!

}

if (p.subAdministrativeArea != nil){

subadministrativearea = p.administrativeArea!

}

if ((p.postalCode) != nil){

postalcode = p.postalCode!

}

if ((p.country) != nil){

country = p.country!

}

self.info = "\(subthoroughFare)\(thoroughfrae)\(sublocality)\(subadministrativearea)\(postalcode)\n\(country)"

}

})

这段代码没有语法错误,但工作效果不好,并且没有显示我的位置。

0