0

我做了一个项目并按照互联网上的教程进行操作。它显示注释并具有自定义标注视图。我添加了一个带有按钮功能的标签,如我的屏幕截图所示。我现在需要一些帮助来获得从我的项目中启动地图应用程序的功能。我在项目中发表了评论,显示了我所做的以及我需要帮助的地方

import UIKit

导入 MapKit

类视图控制器:UIViewController,MKMapViewDelegate,UISearchBarDelegate {

@IBOutlet var mapView: MKMapView!
@IBOutlet var searchBarMap: UISearchBar!

var coordinates: [[Double]]!
var names:[String]!
var addresses:[String]!

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation
    {
        return nil
    }
    if annotation is MKPointAnnotation
    {
        return nil
    }
    var annotationView = self.mapView.dequeueReusableAnnotationView(withIdentifier: "Pin")
    if annotationView == nil{
        annotationView = AnnotationView(annotation: annotation, reuseIdentifier: "Pin")
        annotationView?.canShowCallout = false
    }else{
        annotationView?.annotation = annotation
    }
    annotationView?.image = UIImage(named: "parking-sign")
    return annotationView
}



override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    searchBarMap.delegate = self

    coordinates = [[57.638486,18.299698],[57.636859,18.300468],[57.634807, 18.293389]]// Latitude,Longitude
    names = ["Österport","Åhléns","Söderport/busstation"]
    addresses = ["regler: p-skiva 2 timmar 9-18, oftast få platser lediga","stor med två våningar, övre plan oftast väl fylld och komplicerade parkeringsrutor","Nära om du vill till adelsgatan"]
    self.mapView.delegate = self

    for i in 0...2
    {
        let coordinate = coordinates[i]
        let point = StarbucksAnnotation(coordinate: CLLocationCoordinate2D(latitude: coordinate[0] , longitude: coordinate[1] ))
        point.name = names[i]
        point.address = addresses[i]

        self.mapView.addAnnotation(point)
    }
    let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 57.630615, longitude: 18.303023), span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))
    self.mapView.setRegion(region, animated: true)


}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


func mapView(_ mapView: MKMapView,
             didSelect view: MKAnnotationView)
{
    // 1
    if view.annotation is MKUserLocation
    {
        return
    }
    if view.annotation is MKPointAnnotation
    {
        return

    }

    let starbucksAnnotation = view.annotation as! StarbucksAnnotation
    let views = Bundle.main.loadNibNamed("CustomCalloutView", owner: nil, options: nil)
    let calloutView = views?[0] as! CustomCalloutView
    calloutView.starbucksName.text = starbucksAnnotation.name
    calloutView.starbucksAddress.text = starbucksAnnotation.address
    // declaring label as a button
    let button = UIButton(frame: calloutView.directions.frame)
    button.addTarget(self, action: #selector(ViewController.openInMaps(sender:)), for: .touchUpInside)
    calloutView.addSubview(button)

    calloutView.center = CGPoint(x: view.bounds.size.width / 2, y: -calloutView.bounds.size.height*0.52)
    view.addSubview(calloutView)
    mapView.setCenter((view.annotation?.coordinate)!, animated: true)
}

func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
    if view.isKind(of: AnnotationView.self)
    {
        for subview in view.subviews
        {
            subview.removeFromSuperview()
        }
    }
}
//I want to know what i should fill this function with to open maps and give me directions
func openInMaps(sender: UIButton)
{
    //This is what i tested so far

    let coordinates1 = coordinates
    let point = StarbucksAnnotation(coordinate: CLLocationCoordinate2D(latitude: coordinates1[0] , longitude: coordinates1[1] ))
    let regionDistance:CLLocationDistance = 10000
    let regionSpan = MKCoordinateRegionMakeWithDistance(point, regionDistance, regionDistance)

    let options = [
        MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center),
        MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)
    ]
    let placemark = MKPlacemark(coordinate: point, addressDictionary: nil)
    let mapItem = MKMapItem(placemark: placemark)
    mapItem.name = "destination"
    mapItem.openInMaps(launchOptions: options)
}

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    searchBarMap.resignFirstResponder()

    let geocoder = CLGeocoder()
    geocoder.geocodeAddressString(searchBar.text!) { (placemarks:[CLPlacemark]?, error:Error?) in

        if error == nil {

            let placemark = placemarks?.first

            let anno = MKPointAnnotation()
            anno.coordinate = (placemark?.location?.coordinate)!
            anno.title = self.searchBarMap.text!

            self.mapView.addAnnotation(anno)
            self.mapView.selectAnnotation(anno, animated: true)

        }else{
            print(error?.localizedDescription ?? "error")
        }
    } 
}

}

4

0 回答 0