首页 > 解决方案 > 我以编程方式创建的按钮从我的应用程序中推送 Apple Maps 应用程序停止工作.. 为什么?

问题描述

我有一个正在开发的导航应用程序,它的工作原理类似于普通地图应用程序,允许您通过搜索表等搜索位置。所以在我的应用程序中,我有一个按钮,允许用户添加多个注释(默认为每个当用户单击搜索表中的新结果时,我将其设置为删除最新的注释)并找到所有这些区域之间的中心点。然后代码将该中心点移动到最近的地址,以便有一个地方可以获取方向,而不仅仅是纬度和经度。无论如何,我已经设置好了,当用户点击注释时,它的正上方会出现一个按钮,并且该按钮应该将您发送到已加载预设路线的苹果地图。这直到最近才起作用。以下是执行此按钮操作的代码:

extension ViewController : MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?{
    if annotation is MKUserLocation {
        //return nil so map view draws "blue dot" for standard user location
        return nil
    }
    let reuseId = "pin"
    var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
    pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
    pinView?.pinTintColor = UIColor.red
    pinView?.canShowCallout = true
    let smallSquare = CGSize(width: 30, height: 30)
    let button = UIButton(frame: CGRect(origin: (CGPoint()), size: smallSquare))
    button.setBackgroundImage(UIImage(named: "car"), for: [])
    button.addTarget(self, action: #selector(getDirections), for: UIControlEvents.touchUpInside)

    pinView?.leftCalloutAccessoryView = button
    return pinView
}
}

并在课堂上

@objc func getDirections(){
    if let selectedPin = selectedPin {
        let mapItem = MKMapItem(placemark: selectedPin)
        let launchOptions = [MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving]
        mapItem.openInMaps(launchOptions: launchOptions)
    }
}

目标 C 函数会不会有些奇怪?这是我最近添加的可能导致问题的代码,尽管我真的不知道:

func resolveAddress(for averageCoordinate: CLLocationCoordinate2D, completion: @escaping (MKPlacemark?) -> () ) {

    let geocoder = CLGeocoder()
    let averageLocation = CLLocation(latitude: averageCoordinate.latitude, longitude: averageCoordinate.longitude)
    geocoder.reverseGeocodeLocation(averageLocation) { (placemarks, error) in
        guard error == nil,
            let placemark = placemarks?.first
        else {
            completion(nil)
            return
        }
        completion(MKPlacemark(placemark: placemark ))
    }
}

@IBAction func middleFinderButton(_ sender: Any) {

    let totalLatitude = mapView.annotations.reduce(0) { $0 + $1.coordinate.latitude }

    let totalLongitude = mapView.annotations.reduce(0) { $0 + $1.coordinate.longitude }

    let averageLatitude = totalLatitude/Double(mapView.annotations.count)

    let averageLongitude = totalLongitude/Double(mapView.annotations.count)

    let centerPoint = MKPointAnnotation()

    centerPoint.coordinate.latitude = averageLatitude
    centerPoint.coordinate.longitude = averageLongitude


    mapView.addAnnotation(centerPoint)

    resolveAddress(for: centerPoint.coordinate) { placemark in
        if let placemark = placemark {
            self.mapView.addAnnotation(placemark)
        } else {
            self.mapView.addAnnotation(centerPoint)
        }
    }

    print(totalLatitude)
    print(totalLongitude)
    print(averageLatitude)
    print(averageLongitude)
    print(centerPoint.coordinate)

    }


}

任何人都知道为什么此按钮不起作用并将应用程序推送到具有预设路线的地图?可能是模拟器出现故障(它曾经可以工作,所以我对此表示怀疑)?谢谢。

标签: iosswiftbuttonmapkit

解决方案


推荐阅读