1
Ich versuche, eine Schaltfläche hinzufügen, die Apple Maps
App öffnen wird und Richtungen zu diesem Speicherort geben. Hilfe würde sehr geschätzt werden!Hinzufügen einer Schaltfläche zum Öffnen von Apple Maps, um Wegbeschreibungen zu erhalten
import UIKit
import MapKit
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
@IBOutlet var searchText: UITextField!
var matchingItems: [MKMapItem] = [MKMapItem]()
var destination:MKMapItem = MKMapItem()
override func viewDidLoad() {
super.viewDidLoad()
searchText.hidden = true
}
@IBAction func textFieldReturn(sender: AnyObject) {
sender.resignFirstResponder()
mapView.removeAnnotations(mapView.annotations)
}
@IBAction func fiveMiles(sender: AnyObject) {
changeDistanceView(8046.72)
}
@IBAction func tenMiles(sender: AnyObject) {
changeDistanceView(16093.4)
}
@IBAction func fifthteenMiles(sender: AnyObject) {
changeDistanceView(24140.2)
}
@IBAction func twentyMiles(sender: AnyObject) {
changeDistanceView(32186.9)
}
func changeDistanceView(miles: Double) {
self.performSearch()
let userLocation = mapView.userLocation
let region = MKCoordinateRegionMakeWithDistance(
userLocation.location!.coordinate, miles, miles)
mapView.setRegion(region, animated: true)
}
@IBAction func changeMapView(sender: UIBarButtonItem) {
if mapView.mapType == MKMapType.Standard {
mapView.mapType = MKMapType.Satellite
} else {
mapView.mapType = MKMapType.Standard
}
}
func mapView(mapView: MKMapView, didUpdateUserLocation
userLocation: MKUserLocation) {
mapView.centerCoordinate = userLocation.location!.coordinate
self.performSearch()
let userLocation = mapView.userLocation
let region = MKCoordinateRegionMakeWithDistance(
userLocation.location!.coordinate, 2000, 2000)
mapView.setRegion(region, animated: true)
}
func performSearch() {
matchingItems.removeAll()
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = searchText.text
request.region = mapView.region
let search = MKLocalSearch(request: request)
search.startWithCompletionHandler({
(response: MKLocalSearchResponse?,error: NSError?) in
if error != nil {
print("Error occured in search: \(error!.localizedDescription)")
} else if response!.mapItems.count == 0 {
print("No matches found")
} else {
print("Matches found")
for item in response!.mapItems {
print("Name = \(item.name)")
print("Phone = \(item.phoneNumber)")
self.matchingItems.append(item as MKMapItem)
print("Matching items = \(self.matchingItems.count)")
let annotation = MKPointAnnotation()
annotation.coordinate = item.placemark.coordinate
annotation.title = item.name
annotation.subtitle = item.phoneNumber
self.mapView.addAnnotation(annotation)
}
}
})
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
// Don't want to show a custom image if the annotation is the user's location.
guard !annotation.isKindOfClass(MKUserLocation) else {
return nil
}
let annotationIdentifier = "AnnotationIdentifier"
var annotationView: MKAnnotationView?
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier) {
annotationView = dequeuedAnnotationView
annotationView?.annotation = annotation
}
else {
let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
annotationView = av
}
if let annotationView = annotationView {
// Configure your annotation view here
annotationView.canShowCallout = true
let pinImage = UIImage(named: "pawPrint.png")
let size = CGSize(width: 30, height: 30)
UIGraphicsBeginImageContext(size)
pinImage!.drawInRect(CGRectMake(0, 0, size.width, size.height))
let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
annotationView.image = resizedImage
}
return annotationView
}
}
Sie keinen Code hinzugefügt haben, dass Apple Map App öffnen. –
Überprüfen Sie meine Antwort hier, hoffe es hilft http://Stackoverflow.com/a/40349204/1948163 –