2017-03-15 68 views
1

我有一个mapkit视图,数据正在从前一个视图发送到,这工作正常。我需要了解的是如何将我的源位置设置为用户的当前位置。这是我的代码行代码:mapkit中的当前位置

let sourcelocation = CLLocationCoordinate2D(latitude: track.lat, longitude: track.lon) 
let destinationLocation = CLLocationCoordinate2D(latitude: track.lat, longitude: track.lon) 

当我运行,显然这使2个引脚完全相同的位置。

我不确定要放入sourceLocation行来显示当前位置。

我在我的代码已经下面几行:

VAR myLocation:CLLocationCoordinate2D

self.locationManager.requestAlwaysAuthorization() 
self.locationManager.requestWhenInUseAuthorization() 

if CLLocationManager.locationServicesEnabled() { 
    locationManager.delegate = self 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    locationManager.startUpdatingLocation() 
} 

,我宣布我的委托以:

CLLocationManagerDelegate 

我只是不知道从哪里现在出发。

最终,我试图让它绘制一条路线并允许您从当前位置导航到目标位置。

回答

2

你可以得到当前位置坐标由CLLocationManager为:

让sourcelocation = self.locationManager.location .coordinate

+0

完美谢谢 –

1

由于您使用MapKit,你可能有在某个时候实现一个MapView?如果有意义,您可以使用

mapView.userLocation.location 

获取设备的位置。