gpt4 book ai didi

ios - 添加一个按钮以打开Apple Maps以获取路线

转载 作者:行者123 更新时间:2023-11-28 08:31:27 25 4
gpt4 key购买 nike

我正在尝试添加一个按钮,该按钮将打开AppleMaps应用程序并指示该位置。非常感谢您的帮助!

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
}

}

最佳答案

试试这个希望对你有用

func openMap() {

let lat1 : NSString = self.placeLat
let lng1 : NSString = self.placeLng

let latitute:CLLocationDegrees = lat1.doubleValue
let longitute:CLLocationDegrees = lng1.doubleValue

let regionDistance:CLLocationDistance = 10000
let coordinates = CLLocationCoordinate2DMake(latitute, longitute)
let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
let options = [
MKLaunchOptionsMapCenterKey: NSValue(MKCoordinate: regionSpan.center),
MKLaunchOptionsMapSpanKey: NSValue(MKCoordinateSpan: regionSpan.span)
]
let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = "\(self.placeName)"
mapItem.openInMapsWithLaunchOptions(options)

}

关于ios - 添加一个按钮以打开Apple Maps以获取路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38801177/

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