ios - 如何使用服务器中的数据创建多个 MapKit 注释?

标签 ios iphone swift mapkit

我在 MapView 上进行多个注释时遇到了很大的困难。到目前为止,我已经能够创建相关的类来下载、解析数据并将其存储到可以使用的数组中。然而,我仍然在努力使用上述数据并进行所需的注释。

HomeModel 类 - 从服务器下载并解析所需信息

import UIKit
import Foundation

protocol HomeModelProtocol: class {
func itemsDownloaded(items: NSArray)
}

class HomeModel: NSObject, URLSessionDataDelegate {

weak var delegate: HomeModelProtocol!

var data = Data()

let urlPath: String = "https://FAKEDATABASEURL.XYZ"

func downloadItems() {
    let url: URL = URL(string: urlPath)!
    let defaultSession = Foundation.URLSession(configuration: URLSessionConfiguration.default)
    let task = defaultSession.dataTask(with: url) { (data, response, error) in

        if error != nil {
            print("Failed to download data")
        } else {
            print("Data downloaded")
            self.parseJSON(data!)
        }
    }
    task.resume()
}

func parseJSON(_ data:Data) {

    var jsonResult = NSArray()

    do {
        jsonResult = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) as! NSArray
    } catch let error as NSError {
        print(error)
    }

    var jsonElement = NSDictionary()
    let locations = NSMutableArray()

    for i in 0 ..< jsonResult.count {
        jsonElement = jsonResult[i] as! NSDictionary
        let location = LocationModel()

        if let name = jsonElement["Name"] as? String,
           let address = jsonElement["Address"] as? String,
           let latitude = jsonElement["Latitude"] as? String,
           let longitude = jsonElement["Longitude"] as? String {

            location.name = name
            location.address = address
            location.latitude = latitude
            location.longitude = longitude
    }

    locations.add(location)
}
    DispatchQueue.main.async(execute: { ()-> Void in
        self.delegate.itemsDownloaded(items: locations)
    })
}
}

LocalModel 类 - 将数据存储到应用程序使用的数组中

import UIKit
import Foundation

class LocationModel: NSObject {

// Properties 
var name: String?
var address: String?
var latitude: String?
var longitude: String?

// Empty constructor
override init() { }

// Construct with @name, @address, @latitude and @longitude.
init(name: String, address: String, latitude: String, longitude: String) {
    self.name = name
    self.address = address
    self.latitude = latitude
    self.longitude = longitude
}

// Print the object's current state
override var description: String {

    return "Name: \(String(describing: name)), Address:\(String(describing: address)), Latitude: \(String(describing: latitude)), Longitude: \(String(describing: longitude))"
}
}

map View Controller - 控制应用程序的 map

import UIKit
import MapKit
import CoreLocation

class HotPlacesViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

@IBOutlet weak var mapView: MKMapView!
var isFirstTime = true

var locationManager = CLLocationManager()
let newPin = MKPointAnnotation()

var selectedLocation:LocationModel?

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    // Setup the location services delegate in this class.
    locationManager.delegate = self

    // This little method requests the users permission for location services whilst in this view controller.
    if CLLocationManager.authorizationStatus() == .notDetermined {
        self.locationManager.requestAlwaysAuthorization()
    let alert = UIAlertController(title: "You can change this option in the Settings App", message: "So keep calm your selection is not permanent. 🙂",
                                  preferredStyle: .alert)

        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))

        self.present(alert, animated: true, completion: nil)
    }

    locationManager.distanceFilter = kCLDistanceFilterNone
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.startUpdatingLocation()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

}

// Drops the pin on the users current location.
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    mapView.removeAnnotation(newPin)

    let location = locations.last! as CLLocation
    let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
    if(self.isFirstTime) {
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

    // Set the region on the map.
    mapView.setRegion(region, animated: true)
    self.isFirstTime = false
    }

    newPin.coordinate = location.coordinate
    mapView.addAnnotation(newPin)

}
}

最佳答案

使您的 letlocations = NSMutableArray() 数组可供其他类访问,以便您可以在 HotPlacesViewController 类中使用该数组。

然后在您的 HotPlacesViewController 类中声明一个用于保存位置 数据的属性。并在 viewDidLoad() 中加载数据,如下所示:

class HotPlacesViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
    // declare a property which will hold the locations data
    override func viewDidLoad() {
        ...
        ...
        locations = // load your data here
    }
}

然后,对于多个注释,请遵循以下逻辑:

for location in locations {
    let annotation = MKPointAnnotation()
    annotation.title = location.name
    annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
    mapView.addAnnotation(annotation)

}

关于ios - 如何使用服务器中的数据创建多个 MapKit 注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45009009/

相关文章:

iphone - 当单元格是可变高度时,在 cellForRowAtIndexPath 中获取单元格的高度

android - 仅向新用户显示屏幕 react 导航 v3

iphone - Interface Builder 文件中的未知类 myView

iPhone 语音转文本和返回语音 API

swift - 对 Collection 扩展函数的调用不明确

swift - 将 ARReferenceImage 从阵列上传到 Firebase 存储

iphone - 检测运营商连接类型(3G/EDGE/GPRS)

iphone - 标签栏和 Storyboard : Change the default tab

ios - 重写 ios 方法时何时使用 super

iOS 状态栏覆盖和 PhoneGap 构建