如何在 Swift 4.2 中在地图套件上设置坐标轴



我想要一个像Uber这样的应用程序。我可以保存用户的坐标并显示在表格视图上。但是当我想在地图上显示用户坐标时,不适用于驾驶员方面。我错了什么?

驱动程序控制器:

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)
}

并像这样请求VC:

@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)
}
}
}
}

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

似乎您在故事板中做错了什么。

我已经复制了您的代码并用于我的演示。

请检查一下。

查看控制器,从我通过经纬度的地方

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)
}
}
}
}
}

最新更新