我是初学者,我只是为了好玩而学习Swift。现在我正在研究应用程序中地图的使用,我想将当前坐标(纬度,经度)打印到标签上。
现在,我已设法在地图上显示地图和当前位置并获取其坐标,但我需要帮助我如何将这些坐标打印到标签上。
这是我到目前为止所做的:
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var posizione: UILabel!
//Map
@IBOutlet weak var map: MKMapView!
@IBAction func rileva(_ sender: Any)
{
}
let manager = CLLocationManager()
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let location = locations[0]
let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
let myLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
map.setRegion(region, animated: true)
print(location.coordinate)
print(location.altitude)
self.map.showsUserLocation = true
}
答案 0 :(得分:0)
在didUpdateLocation
功能中:
self.posizione.text = "latitude: " + String(location.coordinate.latitude) + ", longitude: " + String(location.coordinate.longitude)
答案 1 :(得分:0)
嗨试试这段代码:
posizione.text = "latitude : \(location.coordinate.latitude) - longitude : \(location.coordinate.longitude)"
答案 2 :(得分:0)
使用。
self.posizione.text = "Latitude is \(location.coordinate.latitute) and longitude is \(location.coordinate.longitude)".