【发布时间】:2017-09-27 23:46:26
【问题描述】:
我正在Xcode 中创建一个应用程序。应用程序从外部文件中读取coordinates 并在table view 中列出。当用户点击任何列表单元格时,它会打开一个mapkit,它根据单元格的纬度和经度指向coordinate。
到目前为止,我的应用程序已经可以读取外部文件,并显示带有纬度和经度的 table view。它还打开了带有地图的视图,但问题是我无法使用坐标在地图上显示位置。
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,这是正确的。我也尝试根据索引打印,也得到了正确的值。
但是,我无法使用纬度和经度值在地图上显示位置。
请帮忙。
【问题讨论】:
-
“但是,我不能使用纬度和经度值在地图上显示位置”的确切问题是什么?您在哪里尝试“在地图上显示位置”?
-
如果我在第二个视图中 print(selectedName),我得到 "["Latitude": "-27.99824106", "DateandTime": "2010-07-07 22:27:31", "经度”:“153.42096322”]”作为答案。 (值根据列表的索引而变化)我想用这个经纬度去地图上的位置。