ios - 我以编程方式创建的按钮从我的应用程序中推送 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)
}
}
任何人都知道为什么此按钮不起作用并将应用程序推送到具有预设路线的地图?可能是模拟器出现故障(它曾经可以工作,所以我对此表示怀疑)?谢谢。
解决方案
推荐阅读
- xamarin - PropertyChanged 事件在首次运行时不反映新的 Image.Source
- math - 简单的方法来解决许多数字的乘法然后除法
- c# - 如何使用 MSBuild 公开签署项目
- spring-boot - Junit UnitTests没有保存到SpringBoot中的数据库
- python - python,通过 ActiveDirectoryPassword 登录到 db Azure
- python - add_done_call_back() 给出 AttributeError: 'Future' object has no attribute 'select'
- r - 将标签放置在分箱/步骤颜色条指南中的箱中间?
- wordpress - 我们可以在下划线启动主题中编辑 packages.json 吗?
- pip - Spyder5不安装pip?
- flutter - Flutter:在具有 Android 版本(11 API 30)的 Android Emulator 上启动时,ScrollAppBar 显示错误的大小