注释未在地图上显示

时间:2017-02-11 20:27:59

标签: json swift mapkit

我正在尝试使用来自服务器的位置列表在地图上添加注释。

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

我正在使用alamo fire从服务器发送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)
    }
}

出于某种原因,当我运行它时,注释似乎不会显示在地图中。我似乎正在做正确的添加注释的方法,但可能错过了一个小但重要的一点。有人可以帮忙吗?

1 个答案:

答案 0 :(得分: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)
}

因为在viewDidLoad之前调用了viewWillAppear,并且因为Alamofire请求是async,这意味着它将在viewDidLoad和{{之后完成1}}。那么当你添加注释时,你的数组是空的,当你的数据被下载时 - 没有任何反应