2017-02-11 77 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 
    } 
}] 

我使用阿拉莫火從服務器發送GET請求來獲得在上面的viewWillAppear中方法JSON數據。

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做這裏面的請求功能(加載數據後)

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) 
} 

因爲viewDidLoadviewWillAppear之前調用,因爲Alamofire請求async,這意味着,它將viewDidLoad後完成和viewWillAppear。那麼會發生什麼情況是您的陣列是空的,當您添加註釋時,以及您的數據被下載時 - 沒有任何反應

+0

工作。謝謝! – kennanwho