gpt4 book ai didi

ios - 来自外部文件的位置坐标

转载 作者:行者123 更新时间:2023-11-28 15:40:51 25 4
gpt4 key购买 nike

我正在 Xcode 中创建一个应用程序。应用程序从外部文件读取坐标,并在表格 View 中列出。当用户单击任何列表单元格时,它会打开一个 mapkit,它指向根据单元格纬度和经度的 坐标

到目前为止,我的应用程序已经可以读取外部文件,并显示带有纬度和经度的表格 View 。它还会打开 map View ,但问题是我无法使用坐标在 map 上显示位置。

import UIKit
import MapKit


class listLoactionTableViewController: UITableViewController {

//array to store the list
var storeLoc = [String:Any]()

var arrayClient = NSMutableArray ()
var readings: [String] = [""]
//add map view

override func viewDidLoad() {
super.viewDidLoad()

//get the path where the file is stored
let path = Bundle.main.path(forResource: "gps", ofType: "csv")

//add file manager to check is the file exist to avoid crash
let filemgr = FileManager.default

//grab all content from teh file and store into the string
//make an array withfrom the string by seperating line

//iterate the content of the array

//add the objects fr4om the array for display

//add all teh field

//display the amout of line we have title
if filemgr.fileExists(atPath: path!){
do {
let fullText = try String(contentsOfFile: path!, encoding: String.Encoding.utf8)

readings = fullText.components(separatedBy: "\n") as [String]

for i in 0..<readings.count{
let listData = readings[i].components(separatedBy: ";") as [String]

storeLoc["Latitude"] = "\(listData[0])"
storeLoc["Longitude"] = "\(listData[1])"
storeLoc["DateandTime"] = "\(listData[2])"


arrayClient.add(storeLoc)
}
} catch let error as NSError {
print("error: \(error)")
}
}

self.title = "Number of Inputs \(arrayClient.count)"


}

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

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return arrayClient.count
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

let coord = arrayClient[indexPath.row]
cell.detailTextLabel?.text = "Latitude:\((coord as AnyObject).object(forKey: "Latitude")!) Longitude:\((coord as AnyObject).object(forKey: "Longitude")!)"
cell.textLabel?.text = "Date and Time: \((coord as AnyObject).object(forKey: "DateandTime")!)"
return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "oneSegue", sender: arrayClient[indexPath.row])

}

它正确显示所有坐标的列表。当我点击我的应用程序时,它会打开一个带有 mapkit 的 segue

import UIKit
import MapKit

class showMapViewController: UIViewController {

@IBOutlet weak var showMap: MKMapView!

var selectedName = [String:Any]()

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.

}

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


}

我尝试打印“selectedName.count”并得到 3,这是正确的。我也试着根据索引打印,也得到了正确的值。

但是,我无法使用纬度和经度值在 map 上显示位置。

请帮忙。

最佳答案

showMapViewControllerviewDidLoad() 中使用这个例子:

guard let latString =  selectedName["Latitude"] as? String,
let longString = selectedName["Longitude"] as? String,
let latitude = Double(latString),
let longitude = Double(longString)
else {
fatalError("You better handle the error here")
}

let center = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

showMap.setRegion(region, animated: true)

关于ios - 来自外部文件的位置坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43704231/

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