【问题标题】:How to set coordinate axis on mapkit in Swift 4.2如何在 Swift 4.2 中的 mapkit 上设置坐标轴
【发布时间】:2018-08-13 17:50:04
【问题描述】:

我想要一个像 Uber 这样的应用。我可以保存用户的坐标并显示在 tableview 上。但是当我想在地图上显示用户坐标时,不适用于驾驶员侧。我怎么了?

驱动控制器:

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let stoaryboard = UIStoryboard(name: "Main", bundle: nil)
    let requestVC = stoaryboard.instantiateViewController(withIdentifier: "TaxiRequestVC") as! TaxiRequestAcceptViewController
    let snapshot = taksiRequests[indexPath.row]
    if let taxiRequestData = snapshot.value as? [String:AnyObject]{
        if let email = taxiRequestData["email"] as? String{
            if let lat = taxiRequestData["lat"] as? Double{
                if let lon = taxiRequestData["lon"] as? Double{
                    passengerVC.userEmail = email
                    passengerVC.latitude = lat
                    passengerVC.longitude = lon
                }
            }
        }
    }

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

而 RequestVC 是这样的:

@IBOutlet weak var requestMap: MKMapView!
var userEmail:String?
var latitude:Double?
var longitude:Double?

override func viewDidLoad() {
    super.viewDidLoad()
    DispatchQueue.main.async {
        if let lat = self.latitude{
            if let lon = self.longitude{
                let center = CLLocationCoordinate2DMake(lat, lon)
                let span = MKCoordinateSpanMake(0.01, 0.01)
                let region = MKCoordinateRegion(center: center, span: span)
                self.requestMap.setRegion(region, animated: true)
                let annotation = MKPointAnnotation()
                annotation.coordinate = center
                if let email = self.userEmail{
                    annotation.title = email
                }

                self.requestMap.addAnnotation(annotation)
            }
        }
    }
}

如果我打印中心或电子邮件工作正常。但是地图不起作用。你有什么想法吗?

【问题讨论】:

    标签: swift mapkit swift4 mapkitannotation


    【解决方案1】:

    好像你在故事板中做错了什么。

    我已复制您的代码并在我的演示中使用。

    请检查一下。

    查看控制器从我经过 lat long 的位置

    import UIKit
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        @IBAction func gotMap(_ sender: Any) {
            let map = Map()
            map.latitude = 23.0225
            map.longitude = 72.5714
            self.navigationController?.pushViewController(map, animated: true)
        }
    
    }
    

    从我显示地图的位置查看控制器

    import UIKit
    import MapKit
    class Map: UIViewController {
        var requestMap = MKMapView()
        var userEmail:String?
        var latitude:Double?
        var longitude:Double?
    
        override func viewDidLoad() {
            super.viewDidLoad()
            view.addSubview(requestMap)
            requestMap.frame = view.frame
    
            DispatchQueue.main.async {
                if let lat = self.latitude{
                    if let lon = self.longitude{
                        let center = CLLocationCoordinate2DMake(lat, lon)
                        let span = MKCoordinateSpanMake(0.01, 0.01)
                        let region = MKCoordinateRegion(center: center, span: span)
                        self.requestMap.setRegion(region, animated: true)
                        let annotation = MKPointAnnotation()
                        annotation.coordinate = center
                        if let email = self.userEmail{
                            annotation.title = email
                        }
    
                        self.requestMap.addAnnotation(annotation)
                    }
                }
            }
        }
    }
    

    【讨论】:

    • 确保您传递了有效的经度,在您分配经度的位置放置断点。
    • 仅显示地图不起作用。不显示用户位置:/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多