2017-02-11 19 views
0

私は、サーバーから来た場所のリストを使用して地図に注釈を追加しようとしています。注釈が地図上に表示されない

JSONデータ:

[{ "_id" : "589f3e299b64795df23a886b", 
    "name" : "Store 1", 
    "description" : "Most awesome store!", 
    "location" : { 
     "longitude" : -6.279025369998967, 
     "latitude" : 53.35487382895707 
    } 
}, 
{ 
    "_id" : "589f3e299b64795df23a886b", 
    "name" : "Store 2", 
    "description" : "Most awesome store!", 
    "location" : { 
     "longitude" : -6.267085527951536, 
     "latitude" : 53.33785738724761 
    } 
}] 

私はviewWillAppear方法における上記のJSONデータを取得するために、サーバからのGETリクエストを送信するためにアラモ火を使用しています。

override func viewWillAppear(_ animated: Bool) { 
    locationManager.delegate = self 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    locationManager.requestWhenInUseAuthorization() 
    locationManager.requestLocation() 
    mapView.showsUserLocation = false 

    var currentLocation = Location() 
    if let location = locationManager.location { 
     currentLocation = Location(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude) 
    } 

    //default to 10 km radius for now 
    let params = [ 
      "latitude": currentLocation.getLatitude(), 
      "longitude": currentLocation.getLongitude(), 
      "radius": 10.0 
    ] as [String : Any] 

    Alamofire.request("http://website.com/fyp/searchNearbyStores", parameters: params).responseJSON { response in 
     switch response.result { 
     case .success(let value): 
      let result = JSON(value) 
      print(result) 
      self.storeList = result.arrayValue.map({ 

       Store(name: $0["name"].stringValue, description: $0["description"].stringValue, location: CLLocationCoordinate2D(latitude: $0["location"]["latitude"].doubleValue, longitude: $0["location"]["longitude"].doubleValue)) 
      }) 

     case .failure(let error): 
      print(error) 
     } 
    } 
} 

最後にviewDidLoadメソッドでは、注釈を追加するためにlat/longデータとともにstoreListを使用します。

override func viewDidLoad() { 
    super.viewDidLoad() 

    //zoom to current user location 
    if let location = locationManager.location { 
     let span = MKCoordinateSpanMake(0.01, 0.01) 
     let region = MKCoordinateRegion(center: location.coordinate, span: span) 
     mapView.setRegion(region, animated: true) 
    } 
    for store in storeList { 
     let annotation = MKPointAnnotation() 
     annotation.title = store.getName() 
     annotation.subtitle = store.getDescription() 
     annotation.coordinate = store.getLocation() 
     mapView.addAnnotation(annotation) 
    } 
} 

何らかの理由で、注釈が実行時にマップに表示されないようです。私は注釈を追加する正しい方法をしているようだが、おそらく小さくて重要なビットを逃したかもしれない。誰も助けることができますか?

答えて

0

viewWillAppearが間違っています。 (データがロードされた後)、それはviewDidLoad後に終了されることを、意味し、viewDidLoadviewWillAppear前に呼び出されるため、そしてAlamofire要求がasyncあるので、あなたは

if let location = locationManager.location { 
    let span = MKCoordinateSpanMake(0.01, 0.01) 
    let region = MKCoordinateRegion(center: location.coordinate, span: span) 
    mapView.setRegion(region, animated: true) 
} 
for store in storeList { 
    let annotation = MKPointAnnotation() 
    annotation.title = store.getName() 
    annotation.subtitle = store.getDescription() 
    annotation.coordinate = store.getLocation() 
    mapView.addAnnotation(annotation) 
} 

viewDidLoadで要求を作成し、この内部の要求機能を行う必要がありますおよびviewWillAppear。つまり、配列が空で、注釈を追加したとき、およびデータがダウンロードされたときに何も起こりません。

+0

が処理されました。ありがとうございました! – kennanwho

関連する問題