gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:19:11 25 4
gpt4 key购买 nike

我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com