gpt4 book ai didi

ios - 使用 mapKit 的 map 显示相反的方向 (iOS Swift)

转载 作者:搜寻专家 更新时间:2023-11-01 05:40:23 24 4
gpt4 key购买 nike

我正在尝试使用 Apple map 显示从用户当前位置到之前固定位置的说明。这是我的代码...

这里我在 didUpdateLocations 方法中存储了一个名为 carInitalLocation 和 carInitialCoordinate 的全局变量作为初始坐标。

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
// Find location of user
var userLocation:CLLocation = locations[0] as! CLLocation
var latitude = userLocation.coordinate.latitude
var longitude = userLocation.coordinate.longitude
var latDelta:CLLocationDegrees = 0.1
var longDelta: CLLocationDegrees = 0.1
var span: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
var location = CLLocationCoordinate2DMake(latitude, longitude)
var region: MKCoordinateRegion = MKCoordinateRegionMake(location, span)
var coordinate:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude);
carInitialLocation = userLocation;
carInitialCoordinate = coordinate;

self.map.setRegion(region, animated: true)
manager.stopUpdatingLocation()

}

然后我想找到当前位置,启动苹果 map 并提供返回原始固定位置的方向和路线。我尝试使用以下代码执行此操作:

    func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
let selectedLoc = view.annotation
if(control == view.rightCalloutAccessoryView) {
println(view.annotation.title) // annotation's title
println(view.annotation.subtitle)
println("Annotation '\(selectedLoc.title!)' has been selected")
performSegueWithIdentifier("detailViewSegue", sender: self)

} else {
if(control == view.leftCalloutAccessoryView) {
let currentLocMapItem = MKMapItem.mapItemForCurrentLocation()
let selectedPlacemark = MKPlacemark(coordinate: selectedLoc.coordinate, addressDictionary: nil)
let selectedMapItem = MKMapItem(placemark: selectedPlacemark);
let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeWalking]
var placemarkStartingLocation:MKPlacemark = MKPlacemark(coordinate: carInitialCoordinate, addressDictionary: nil)
var startingLocationItem:MKMapItem = MKMapItem(placemark: placemarkStartingLocation);
let mapItems = [startingLocationItem, currentLocMapItem]

MKMapItem.openMapsWithItems(mapItems, launchOptions:launchOptions)
}
}

一切正常,但问题是,它不是将方向从当前位置映射到初始固定位置,而是将其从初始固定位置映射到当前位置,这与我想要的相反。

如有任何帮助,我们将不胜感激。

最佳答案

您可以像这样切换 mapItems 中对象的顺序

let mapItems = [currentLocMapItem, startingLocationItem]

或者您可以将对 openMapsWithItems(_:launchOptions:) 的调用更改为仅使用一个项目。

MKMapItem.openMapsWithItems([startingLocationItem], launchOptions:launchOptions)

根据MKMapItem Class Reference ,

If you specify the MKLaunchOptionsDirectionsModeKey option in the launchOptions dictionary, the mapItems array must have no more than two items in it. If the array contains one item, the Maps app generates directions from the user’s current location to the location specified by the map item. If the array contains two items, the Maps app generates directions from the location of the first item to the location of the second item in the array.

但是,您似乎想要指定 MKLaunchOptionsDirectionsModeKey 选项,以便 map 显示步行方向。因此,您应该选择第一种解决方案。

关于ios - 使用 mapKit 的 map 显示相反的方向 (iOS Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31309308/

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