2017-06-14 7 views
0

座標を配列に格納しようとしています。コードは正常に実行されますが、新しい実装座標の各反復の後も、配列の数は変わりません。挿入時に配列に格納されていない座標

let manager = CLLocationManager() 

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    let location = locations[0] 
    let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01,0.01) //shows the size of map screen 
    let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude,location.coordinate.longitude) 
    let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span) 
    map.setRegion(region, animated: true) 
    self.map.showsUserLocation = true 
    let LAT = Double(location.coordinate.latitude) 
    let LONG = Double(location.coordinate.longitude) 
    var locationArray = [Double]() 
    locationArray.insert(contentsOf: [LAT, LONG], at: 0) 
    print(locationArray.count) 

答えて

0

これは、各繰り返しで新しいlocationArrayを作成するために発生しています。 locationArrayを、座標を挿入している更新の範囲外に宣言する必要があります。

var locationArray = [Double]() 

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    let location = locations[0] 
    let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01,0.01) //shows the size of map screen 
    let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude,location.coordinate.longitude) 
    let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span) 
    map.setRegion(region, animated: true) 
    self.map.showsUserLocation = true 
    let LAT = Double(location.coordinate.latitude) 
    let LONG = Double(location.coordinate.longitude) 
    locationArray.insert(contentsOf: [LAT, LONG], at: 0) 
    print(locationArray.count) 
} 
+1

すぐに働いてくれてありがとう! –

関連する問題