Tôi có 2000 vị trí trong cơ sở dữ liệu web mà người dùng có thể chọn trên Bản đồ. Tôi có thể yêu cầu cơ sở dữ liệu web chỉ cung cấp cho tôi một số vị trí nhất định bắt nguồn từ vị trí hiện tại.Cách tải vị trí tốt nhất cho MapView từ Webserver
Để làm cho mọi thứ mượt mà và thanh lịch, trước tiên tôi sẽ khởi tạo MKMapView, bắt đầu CLLocationManager và đợi cho đến khi tôi nhận được một didUpdateLocations. Sau đó, tôi sẽ cố gắng để có được dữ liệu của tôi từ một cơ sở dữ liệu với một xử lý hoàn thành.
Tôi có nên
a) có được tất cả dữ liệu cùng một lúc
b) lấy dữ liệu trong mảnh nhỏ hoặc khối?
Cách tốt nhất là gì?
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
self.gotoCurrentLocation()
if let userLocation = manager.location {
GroundHelper.getAllGroundLocations(userLocation) { self.handleWaypoints($0!) }
}
}
private func handleWaypoints(grounds: [Ground]) {
mapView.addAnnotations(grounds)
}
// MARK: - Helper Methods
typealias GPXCompletionHandler = ([Ground]?) -> Void
class func getAllGroundLocations(userlocation: CLLocation, completionHandler: GPXCompletionHandler) {
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0), {()->() in
var results = RestApiManager.sharedInstance.getGPS(userlocation, limit: 50)
// return first 50 results
dispatch_async(dispatch_get_main_queue(), {
var grounds = [Ground]()
for result in results {
let (_,jsonGround) = result
let ground = Ground(json:jsonGround)
grounds.append(ground)
}
completionHandler(grounds)
})
// get the rest
results = RestApiManager.sharedInstance.getGPS(userlocation)
// return them
dispatch_async(dispatch_get_main_queue(), {
var grounds = [Ground]()
for result in results {
let (_,jsonGround) = result
let ground = Ground(json:jsonGround)
grounds.append(ground)
}
completionHandler(grounds)
})
})
}
Đây có phải là điều một lần không? Nói cách khác, bạn sẽ lưu trữ tất cả nội dung đó ngay sau lần đầu tiên ứng dụng tải chứ? –