2015-02-03 5 views
0

그래서 내 ViewController.swift의 기능 안에서 내 위치를 얻으려고합니다.Swift를 사용하여 위치를 올바르게 가져올 수 없습니다.

var manager : CLLocationManager! 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     manager = CLLocationManager() 
     manager.delegate = self 
     manager.desiredAccuracy = kCLLocationAccuracyHundredMeters 
     manager.requestAlwaysAuthorization() 
     manager.startUpdatingLocation() 
     } 

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { 
     var locValue: CLLocationCoordinate2D = manager.location.coordinate 
     println("locations = \(locValue.longitude) \(locValue.latitude)") 
     manager.stopUpdatingLocation() 
    } 

    @IBAction func refresh() { 
     getWeatherData() 
     refreshButton.hidden = true 
     acitivityIndicatorView.hidden = false 
     acitivityIndicatorView.startAnimating() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     } 

     func getWeatherData() -> Void { 

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [AnyObject]!) { 
       var locValue: CLLocationCoordinate2D = manager.location.coordinate 
       println("locations = \(locValue.longitude) \(locValue.latitude)") 
       manager.stopUpdatingLocation() 

동안 : 기능을 토글 버튼을 누른 상태에서

import UIKit 
import CoreLocation 


class ViewController: UIViewController, CLLocationManagerDelegate { 

    var manager : CLLocationManager! 


    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view, typically from a nib. 

     manager = CLLocationManager() 
     manager.delegate = self 
     manager.desiredAccuracy = kCLLocationAccuracyHundredMeters 
     manager.requestAlwaysAuthorization() 
     manager.startUpdatingLocation() 
    } 

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

     var locValue : CLLocationCoordinate2D 
     locValue = manager.location.coordinate 
     println("Coordinates = \(locValue.longitude) + \(locValue.latitude)") 
     manager.stopUpdatingLocation() 
    } 

는하지만, 내가 뭘하려고 오전 그것을 얻을 것이다 :이처럼하려고 할 때

강령은 내가 작품을 사용 중단 점을 사용하면 locationManager를 func한다는 것을 깨달았지만 getweatherdata() 함수의 끝으로 이동합니다.

왜 작동하지 않는지 알 수 없습니다.

답변

1

당신이하고있는 일은 불가능합니다. 당신이 할 수있는 일은 안에 manager.startUpdatingLocation()을 호출하고 업데이트 된 위치 데이터로 didUpdateLocations이 호출 될 때까지 기다리는 것입니다.

@IBAction func refresh() { 
    manager.startUpdatingLocation() 
    refreshButton.hidden = true 
    acitivityIndicatorView.hidden = false 
    activityIndicatorView.hidesWhenStopped = true 
    acitivityIndicatorView.startAnimating() 
} 

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { 
    var locValue: CLLocationCoordinate2D = manager.location.coordinate 
    println("locations = \(locValue.longitude) \(locValue.latitude)") 
    manager.stopUpdatingLocation() 
    refreshButton.hidden = false 
    acitivityIndicatorView.stopAnimating() 

} 
+1

난 당신이 리프레시 기능 : –

+0

넵에 manager.startUpdatingLocation()에 의해 getWeatherData()를 변경하는 것을 잊었다 생각합니다. 그게 내 바보 같았 어. 고마워요 :) – rakeshbs

+0

와우, 고마워요! 나는 그것에 대해서 생각조차하지 못했지만, 그것이 의미있는 것은 맞습니다. 이제는 모두 작동합니다. 감사합니다! – Kvn92

관련 문제