gpt4 book ai didi

ios - 如何将当前位置设置为 request.source Swift 3

转载 作者:行者123 更新时间:2023-11-28 06:24:04 26 4
gpt4 key购买 nike

好的,希望是一个简单快速的问题。

我正在构建一个使用 Apple map 并且工作正常的 mapView,我的 segue 将纬度和经度注入(inject) map ,我想从当前位置导航到包含纬度和经度值的注释。

我已经设法让当前位置显示在 map 上并显示我的注释,但我不确定如何在以下代码行中将我的当前位置设置为源点。

request.source = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: lat,longitude: lon)))

这使用了我当前从我的 segue 注入(inject)的 lat 和 lon 变量。我希望 request.source 行是我的当前位置,lat 和 lon vars 是我已经拥有的目的地。

我只是不确定使 request.source 成为我当前位置的语法。

这是我的整个VC代码供引用:

导入 UIKit导入 MapKit导入核心位置

类 LocateVC:UIViewController,MKMapViewDelegate {

@IBOutlet weak var mapView: MKMapView!
var lat: Double!
var lon: Double!
var name: String!
var locationManager = CLLocationManager()

func checkLocationAuthorizationStatus() {
if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
mapView.showsUserLocation = true
} else {
locationManager.requestWhenInUseAuthorization()
}
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
checkLocationAuthorizationStatus()
}

override func viewDidLoad() {
super.viewDidLoad()

annotation.coordinate = CLLocationCoordinate2D(latitude: lat, longitude: lon)
mapView.addAnnotation(annotation)


let request = MKDirectionsRequest()
request.source = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: lat,longitude: lon)))
request.destination = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: lat,longitude: lon)))
request.requestsAlternateRoutes = true
request.transportType = .automobile

let directions = MKDirections(request: request)

directions.calculate {[unowned self] response, error in
guard let unwrappedResponse = response else { return }

for route in unwrappedResponse.routes {
self.mapView.add(route.polyline)
self.mapView.setVisibleMapRect(route.polyline.boundingMapRect, animated: true)
}
}
}

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(polyline: overlay as! MKPolyline)
renderer.strokeColor = UIColor.blue
return renderer
}

let regionRadius: CLLocationDistance = 1000
let annotation = MKPointAnnotation()
func centerMapOnLocation(location: CLLocation) {
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, regionRadius * 2.0, regionRadius * 2.0)
mapView.setRegion(coordinateRegion, animated: true)
mapView.showsUserLocation = true

}

最佳答案

您必须覆盖 CLLocationManager.didUpdateLocations 才能在位置管理器检索当前位置时得到通知。您必须应用 CLLocationManager 委托(delegate)。

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
let location = locations.last as CLLocation

let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

self.map.setRegion(region, animated: true)
}

关于ios - 如何将当前位置设置为 request.source Swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42512975/

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