我正在尝试显示一个带有两个标记的地图,这些标记具有从 API 检索到的经度和纬度。
获取经度和纬度的函数被调用Viewdidload
override func viewDidLoad() {
super.viewDidLoad()
getCurrent()
还有用于显示带有标记的地图的代码,viewdidload
因此我的完整代码如下所示:
var longFamily = ""
let latFamily = ""
var latShop = ""
var longShop = ""
override func viewDidLoad() {
super.viewDidLoad()
getCurrent()
let coordinate₀ = CLLocation(latitude: CLLocationDegrees(Int(latFamily)!), longitude: CLLocationDegrees(Int(longFamily)!))
let coordinate₁ = CLLocation(latitude: (Int(latFamily)!, longitude: (Int(longFamily)!))
let distanceInMeters = coordinate₀.distance(from: coordinate₁) // result is in meters
let floatDistance = Float(distanceInMeters)
// get two markers with shop and client locations
map.delegate = self
// 2.
let sourceLocation = CLLocationCoordinate2D(latitude: (Int(latFamily)!, longitude: (Int(longFamily)!)
let destinationLocation = CLLocationCoordinate2D(latitude: (Int(latShop)!, longitude: (Int(latShop)!)
我知道在地图加载之前我必须做一些事情来获取数据,但不确定在哪里。感谢您的帮助
GetCurrent 函数调用 API:
Alamofire.request(url!, method: .get, parameters: param,encoding: URLEncoding.default, headers: headers).responseJSON { response in
if let value: AnyObject = response.result.value as AnyObject? {
//Handle the results as JSON
let data = JSON(value)
self.LongShop = data["shopLong"]
// this is for family locations too