2016-04-17 15 views
-2

didUpdateLocations以外の座標を使用しようとしていますが、動作させることができません。didUpdateLocation関数の外部で座標を使用するにはどうすればよいですか?

私は座標を関数外の度数として宣言しようとしますが、それは機能しません。

これについていくつかガイダンスを得ることができますか?

var currentUserLatitude:CLLocationDegrees? 
var currentUserLongitude:CLLocationDegrees? 

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){ 
    //location info about the user 
    let location = locations.last 
    let center = CLLocationCoordinate2D(latitude: (location?.coordinate.latitude)!, longitude: (location?.coordinate.longitude)!) 
    let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05) 
    let region = MKCoordinateRegion(center: center, span:span) 
    self.mapView.setRegion(region, animated: true) 
// i want to use these variables outside the function 
    var currentUserLatitude = location!.coordinate.latitude 
    var currentUserLongitude = location!.coordinate.longitude 

} 
+0

は何この質問は、Javaと関係があるのでしょうか? –

+0

「動作しません」と明確に定義します。 – rmaddy

答えて

1

ローカル関数の外で宣言された変数に値を代入する場合は、変数をローカル変数として再宣言しないでください。

変更し、これらの行:

var currentUserLatitude = location!.coordinate.latitude 
var currentUserLongitude = location!.coordinate.longitude 

へ:

currentUserLatitude = location!.coordinate.latitude 
currentUserLongitude = location!.coordinate.longitude 
+0

rmaddyの答えが正しいです。あなたはそれを受け入れるべきです。単語 "var"または "let"を使用すると、新しい変数が作成されます。コードには2つの異なる変数の組があります。 1組はインスタンス変数で、2組目はdidUpdateLocations関数のローカル変数です。 didUpdateLocations関数内の "var"キーワードを取り除くことによって、あなたのコードはあなたが望むようにインスタンス変数を変更しています。 –

関連する問題