【问题标题】:Location Coordinates from an external file来自外部文件的位置坐标
【发布时间】: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”]”作为答案。 (值根据列表的索引而变化)我想用这个经纬度去地图上的位置。

标签: ios swift swift3 mapkit


【解决方案1】:

showMapViewControllers viewDidLoad() 中使用此示例:

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)

【讨论】:

  • 抱歉,此解决方案不起作用。我知道这是在地图上显示位置的方式。但是,这里的问题是我试图从外部文件中注入纬度和经度。我正在寻找如何从外部文件中注入纬度和经度的解决方案。
  • @Mill3r 对不起。我误解了你的问题,而不是当你写的“它正确显示所有坐标的列表”时,我认为你从文件中正确获取了坐标。添加了一些代码来回答。
  • 抱歉不清楚。 1. 我可以读取 cvs 文件并生成正确显示列表的 tableview。 2. 当我点击一个单元格时,它会打开另一个视图来显示地图。 3.我通过segue准备传递的值的形式是“[“纬度”:“-27.99824106”,“日期和时间”:“2010-07-07 22:27:31”,“经度”:“ 153.42096322"]" 4. 我无法从该字符串中提取此纬度和经度值以将其注入地图 CCLocationCoordinates
  • 我找到了解决方案。我不得不使用 let latitude = Double((selectedName["Latitude"] as AnyObject) as!String)!让 longitude = Double((selectedName["Longitude"] as AnyObject) as!String)!谢谢你的帮助..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-09
  • 2021-05-15
  • 1970-01-01
  • 1970-01-01
  • 2015-08-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多