我想要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)
}
和这样的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)
}
}
}
}
如果我打印中心或电子邮件正常工作。但是地图不起作用。你有什么主意吗?
答案 0 :(得分:0)
似乎您在情节提要板上做错了事。
我已经复制了您的代码并在演示中使用了。
请检查。
我从纬度过去的地方查看控制器
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)
}
}
}
}
}
答案 1 :(得分:0)
我认为您忘记添加CLLocationManager
委托方法,GitHub
并在info.plist中添加see this tutorial this will help you.以访问mapview。