如何在Mapkit Swift 3中绘制当前位置和目的地之间的路线?

vddsk6oq  于 2022-10-31  发布在  Swift
关注(0)|答案(1)|浏览(208)

我正在尝试获取当前用户位置的CLLocationManager示例的经纬度坐标。
在我的viewWillAppear方法中有这两行代码:

  1. locationManager = CLLocationManager()
  2. locationManager.requestWhenInUseAuthorization()

然后我有一个名为startRoute的方法来计算从当前位置到Map上某个特定注解的路线。然后我在这些路径之间绘制路线。不幸的是,这是与Map上的两个注解一起工作的,但我不能让它与用户的当前位置和一些注解一起工作。
我尝试了下面的脚本,但当我打印print("LAT \(currentLocation.coordinate.latitude)")时,它会给予我0.0作为结果。

  1. func startRoute() {
  2. // Set the latitude and longtitude of the locations (markers)
  3. let sourceLocation = CLLocationCoordinate2D(latitude: currentLocation.coordinate.latitude, longitude: currentLocation.coordinate.longitude)
  4. let destinationLocation = CLLocationCoordinate2D(latitude: sightseeing.latitude!, longitude: sightseeing.longitude!)
  5. // Create placemark objects containing the location's coordinates
  6. let sourcePlacemark = MKPlacemark(coordinate: sourceLocation, addressDictionary: nil)
  7. let destinationPlacemark = MKPlacemark(coordinate: destinationLocation, addressDictionary: nil)
  8. // MKMapitems are used for routing. This class encapsulates information about a specific point on the map
  9. let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
  10. let destinationMapItem = MKMapItem(placemark: destinationPlacemark)
  11. // Annotations are added which shows the name of the placemarks
  12. let sourceAnnotation = MKPointAnnotation()
  13. sourceAnnotation.title = "Times Square"
  14. if let location = sourcePlacemark.location {
  15. sourceAnnotation.coordinate = location.coordinate
  16. }
  17. let destinationAnnotation = MKPointAnnotation()
  18. destinationAnnotation.title = "Title"
  19. destinationAnnotation.subtitle = "Subtitle"
  20. if let location = destinationPlacemark.location {
  21. destinationAnnotation.coordinate = location.coordinate
  22. }
  23. // The annotations are displayed on the map
  24. self.mapView.showAnnotations([sourceAnnotation,destinationAnnotation], animated: true )
  25. // The MKDirectionsRequest class is used to compute the route
  26. let directionRequest = MKDirectionsRequest()
  27. directionRequest.source = sourceMapItem
  28. directionRequest.destination = destinationMapItem
  29. directionRequest.transportType = .walking
  30. // Calculate the direction
  31. let directions = MKDirections(request: directionRequest)
  32. // The route will be drawn using a polyline as a overlay view on top of the map.
  33. // The region is set so both locations will be visible
  34. directions.calculate {
  35. (response, error) -> Void in
  36. guard let response = response else {
  37. if let error = error {
  38. print("Error: \(error)")
  39. }
  40. return
  41. }
  42. let route = response.routes[0]
  43. self.mapView.add((route.polyline), level: MKOverlayLevel.aboveRoads)
  44. let rect = route.polyline.boundingMapRect
  45. self.mapView.setRegion(MKCoordinateRegionForMapRect(rect), animated: true)
  46. }
  47. }

在顶部,直接在类声明之后,我创建了对location manager和'CLLocation'的引用:

  1. var locationManager: CLLocationManager!
  2. var currentLocation = CLLocation()

毕竟,这是行不通的。只有当我将sourceLocation更改为硬编码坐标时,它才会绘制到目的地的路线。我做错了什么,或者我遗漏了什么?

4szc88ey

4szc88ey1#

但当我打印print("LAT \(currentLocation.coordinate.latitude)")时,结果会给予0.0
当然了。不然还能做什么?你有房产

  1. var currentLocation = CLLocation()

这是一个零位置。你 * 没有 * 代码可以 * 改变 * 它。因此,它 * 总是 * 一个零位置。
你说你想要
当前用户的位置
但是没有任何代码可以 * 获取 * 用户的位置。

相关问题