【问题标题】:Maps using mapKit displays the opposite directions (iOS Swift)使用 mapKit 的地图显示相反的方向(iOS Swift)
【发布时间】:2015-07-09 06:00:43
【问题描述】:

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

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

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()

}

然后我想找到当前位置,启动苹果地图并提供返回原始固定位置的路线和路线。我尝试使用下面的代码来做到这一点:

    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)
            }
        }

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

任何帮助将不胜感激。

【问题讨论】:

    标签: ios swift maps mapkit


    【解决方案1】:

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

    let mapItems = [currentLocMapItem, startingLocationItem]
    

    或者您可以将呼叫更改为 openMapsWithItems(_:launchOptions:) 以仅使用一项。

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

    根据MKMapItem Class Reference

    如果您在 launchOptions 字典中指定 MKLaunchOptionsDirectionsModeKey 选项,则 mapItems 数组中的项目不得超过两个。如果数组包含一项,则地图应用程序会生成从用户当前位置到地图项指定位置的路线。如果数组包含两项,则地图应用会生成从数组中第一项位置到第二项位置的路线。

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

    【讨论】:

      猜你喜欢
      • 2012-08-15
      • 1970-01-01
      • 1970-01-01
      • 2017-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多