如何通过传递地址打开Apple Maps驾驶方向

时间:2016-12-20 22:55:57

标签: ios swift mapkit apple-maps

我试图通过传递实际地址来实现打开Apple Map应用程序的驾驶方向屏幕的功能。

地址不仅仅是随机地址,而是商业地址,这些地址主要在Apple地图上注册和搜索。因此,通过传递地址,它应该匹配并正确显示该业务的方向,而不是指向地图上的未知注释。当我直接传递经度和纬度时会出现这种情况,因此我不想使用geocodeAddressString()将地址转换为地理协调。

我怎样才能实现它?

2 个答案:

答案 0 :(得分:7)

只需使用Apple Maps API即可。如果您可以通过在Apple Maps中显示其名称来查找业务,则可以通过API找到它。在您的情况下,正确的参数是daddr,如下所示:

http://maps.apple.com/?daddr=1+Infinite+Loop,+Cupertino,+CA

您可以组合多个参数,例如您的起始位置:

http://maps.apple.com/?saddr=1024+Market+St,+San+Francisco,+CA&daddr=1+Infinite+Loop,+Cupertino,+CA

您可以找到支持的参数列表here

请务必通过UIApplication.shared().open(url: URL, options: [String: AnyObject], completionHandler: ((Bool) -> Void)?) - 在iOS 10中打开网址 - 或UIApplication.shared.open(url: URL)

答案 1 :(得分:1)

您可以使用网址调用地图(详见Nicola),但也有适当的API:MKMapItem

  1. 基本上您为POI /地址[1或更多]
  2. 创建地图项目
  3. 用它们打开地图[使用launchOptions开始路线]

    func showMap() {
        //---
        //create item 1
    
        //address
        let coords = CLLocationCoordinate(coordinateField.doubleValue!,coordinateField.doubleValue!)
        let addressDict =
               [CNPostalAddressStreetKey: address.text!,
                CNPostalAddressCityKey: city.text!,
                CNPostalAddressStateKey: state.text!,
                CNPostalAddressPostalCodeKey: zip.text!]        
        //item
        let place = MKPlacemark(coordinate: coords!,
                                 addressDictionary: addressDict)
        let mapItem = MKMapItem(placemark: place)
    
        //---
        //create item 2
    
        //address
        let coords2 = CLLocationCoordinate(coordinateField2.doubleValue!,coordinateField2.doubleValue!)
        let addressDict2 =
               [CNPostalAddressStreetKey: address2.text!,
                CNPostalAddressCityKey: city2.text!,
                CNPostalAddressStateKey: state2.text!,
                CNPostalAddressPostalCodeKey: zip2.text!]
        //item2
        let place2 = MKPlacemark(coordinate: coords2!,
                                 addressDictionary: addressDict2)
        let mapItem2 = MKMapItem(placemark: place2)
    
        //-----
        //launch it     
        let options = [MKLaunchOptionsDirectionsModeKey:
                            MKLaunchOptionsDirectionsModeDriving]
    
        //for 1 only.       
        mapItem.openInMaps(launchOptions: options)
        //for 1 or N items 
        var mapItems = [mapItem, mapItem2] //src to destination
        MKMapItem.openMaps(withItems:mapItems, launchOptions: options)
    }