我正在尝试获取当前用户位置的CLLocationManager示例的经纬度坐标。
在我的viewWillAppear方法中有这两行代码:
locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization()
然后我有一个名为startRoute
的方法来计算从当前位置到Map上某个特定注解的路线。然后我在这些路径之间绘制路线。不幸的是,这是与Map上的两个注解一起工作的,但我不能让它与用户的当前位置和一些注解一起工作。
我尝试了下面的脚本,但当我打印print("LAT \(currentLocation.coordinate.latitude)")
时,它会给予我0.0
作为结果。
func startRoute() {
// Set the latitude and longtitude of the locations (markers)
let sourceLocation = CLLocationCoordinate2D(latitude: currentLocation.coordinate.latitude, longitude: currentLocation.coordinate.longitude)
let destinationLocation = CLLocationCoordinate2D(latitude: sightseeing.latitude!, longitude: sightseeing.longitude!)
// Create placemark objects containing the location's coordinates
let sourcePlacemark = MKPlacemark(coordinate: sourceLocation, addressDictionary: nil)
let destinationPlacemark = MKPlacemark(coordinate: destinationLocation, addressDictionary: nil)
// MKMapitems are used for routing. This class encapsulates information about a specific point on the map
let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
let destinationMapItem = MKMapItem(placemark: destinationPlacemark)
// Annotations are added which shows the name of the placemarks
let sourceAnnotation = MKPointAnnotation()
sourceAnnotation.title = "Times Square"
if let location = sourcePlacemark.location {
sourceAnnotation.coordinate = location.coordinate
}
let destinationAnnotation = MKPointAnnotation()
destinationAnnotation.title = "Title"
destinationAnnotation.subtitle = "Subtitle"
if let location = destinationPlacemark.location {
destinationAnnotation.coordinate = location.coordinate
}
// The annotations are displayed on the map
self.mapView.showAnnotations([sourceAnnotation,destinationAnnotation], animated: true )
// The MKDirectionsRequest class is used to compute the route
let directionRequest = MKDirectionsRequest()
directionRequest.source = sourceMapItem
directionRequest.destination = destinationMapItem
directionRequest.transportType = .walking
// Calculate the direction
let directions = MKDirections(request: directionRequest)
// The route will be drawn using a polyline as a overlay view on top of the map.
// The region is set so both locations will be visible
directions.calculate {
(response, error) -> Void in
guard let response = response else {
if let error = error {
print("Error: \(error)")
}
return
}
let route = response.routes[0]
self.mapView.add((route.polyline), level: MKOverlayLevel.aboveRoads)
let rect = route.polyline.boundingMapRect
self.mapView.setRegion(MKCoordinateRegionForMapRect(rect), animated: true)
}
}
在顶部,直接在类声明之后,我创建了对location manager
和'CLLocation'的引用:
var locationManager: CLLocationManager!
var currentLocation = CLLocation()
毕竟,这是行不通的。只有当我将sourceLocation更改为硬编码坐标时,它才会绘制到目的地的路线。我做错了什么,或者我遗漏了什么?
1条答案
按热度按时间4szc88ey1#
但当我打印
print("LAT \(currentLocation.coordinate.latitude)")
时,结果会给予0.0
当然了。不然还能做什么?你有房产
这是一个零位置。你 * 没有 * 代码可以 * 改变 * 它。因此,它 * 总是 * 一个零位置。
你说你想要
当前用户的位置
但是没有任何代码可以 * 获取 * 用户的位置。