gpt4 book ai didi

swift - 如何在 Swift 4.2 中的 Mapkit 上设置坐标轴

转载 作者:行者123 更新时间:2023-11-30 11:15:00 25 4
gpt4 key购买 nike

我想要一个像 Uber 这样的应用程序。我可以保存用户的坐标并显示在表格 View 上。但是当我想在 map 上显示用户坐标时,对于驾驶员侧不起作用。我有什么错吗?

驱动 Controller :

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

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

最佳答案

似乎您在 Storyboard中做错了什么。

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

请检查一下。

从我传递经纬度的位置查看 Controller

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

}

从我显示 map 的位置查看 Controller

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

关于swift - 如何在 Swift 4.2 中的 Mapkit 上设置坐标轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51827927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com