2017-02-11 79 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