gpt4 book ai didi

ios - 使用我的 ios 应用程序 iOS 9 Swift 2.2 中的指示打开 Apple map 应用程序

转载 作者:搜寻专家 更新时间:2023-10-31 22:20:36 24 4
gpt4 key购买 nike

我一直在寻找很多教程\帮助但似乎没有找到,所以我再次来到这里....我想打开 Apple Maps 以获取路线并从用户当前位置导航到选定的 Pin 当我在我的应用程序中按下自定义 leftCalloutAccessory

我已经设置了按钮,但无法使该功能正常工作,所以请'如果有人可以指导我完成或帮助我使用代码,那将是救命稻草!谢谢

这是我的代码:

    import UIKit
import MapKit
import CoreLocation

class MapViewController: UIViewController,MKMapViewDelegate, CLLocationManagerDelegate,UISearchBarDelegate{

@IBOutlet weak var mapView: MKMapView!

let locationManager = CLLocationManager()

override func viewDidLoad() {
super.viewDidLoad()


//==========RegionLocation : =========

// Init the zoom level
let coordinate:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 31.30, longitude: 34.45)
let span = MKCoordinateSpanMake(125, 125)
let region = MKCoordinateRegionMake(coordinate, span)
self.mapView.setRegion(region, animated: true)



//====================================\\
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
//Dispose of resources that can be re created.

}

//Mark : Location

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])



{
let location = locations.last

let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)

let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.06, longitudeDelta: 0.06))

self.mapView.setRegion(region, animated: true)

self.locationManager.stopUpdatingLocation()
}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
{
print("Errors: " + error.localizedDescription)
}

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let reuseID = "pin"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseID) as? MKPinAnnotationView
if(pinView == nil) {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseID)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
pinView!.rightCalloutAccessoryView = UIButton(type: UIButtonType.DetailDisclosure) as UIButton
let smallSquare = CGSize(width: 30, height: 30)
let button = UIButton(frame: CGRect(origin: CGPointZero, size: smallSquare))
button.setBackgroundImage(UIImage(named: "Car"), forState: .Normal)
pinView?.leftCalloutAccessoryView = button
}
else
{
pinView!.annotation = annotation
}


return pinView

}

func mapView(MapView: MKMapView, annotationView: MKAnnotationView, calloutAccessoryControlTapped Control: UIControl) {

if Control == annotationView.leftCalloutAccessoryView {

}

}

最佳答案

以下函数将打开 Apple map 应用程序并显示从用户当前位置到坐标指定目的地的行车路线。目的地名称显示在 map 应用的To:字段中。

func openMapsAppWithDirections(to coordinate: CLLocationCoordinate2D, destinationName name: String) {
let options = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: nil)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = name // Provide the name of the destination in the To: field
mapItem.openInMapsWithLaunchOptions(options)
}

您可以从您的代码中调用此函数,如下所示:

func mapView(MapView: MKMapView, annotationView: MKAnnotationView, calloutAccessoryControlTapped Control: UIControl) {

if Control == annotationView.leftCalloutAccessoryView {
if let annotation = annotationView.annotation {
// Unwrap the double-optional annotation.title property or
// name the destination "Unknown" if the annotation has no title
let destinationName = (annotation.title ?? nil) ?? "Unknown"
openMapsAppWithDirections(to: annotation.coordinate, destinationName: destinationName)
}
}

}

关于ios - 使用我的 ios 应用程序 iOS 9 Swift 2.2 中的指示打开 Apple map 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38967259/

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