【发布时间】:2018-10-15 21:47:41
【问题描述】:
我正在开发一个导航应用程序,它的工作方式类似于普通地图应用程序,允许您通过搜索表等搜索位置。在应用程序中,用户可以通过单击我的可搜索结果中他们想要的结果来创建注释.
问题:当用户点击注释时,它上方应该会出现一个弹出按钮,因为我已经以编程方式创建了它。单击后,它应该将用户推送到 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)
}
}
Objective-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)
}
github链接:github.com/derekvandermark/VHNavigationAppOfficial-v-0.93
有人知道为什么此按钮不起作用并将应用程序推送到具有预设路线的地图吗?会不会是模拟器出问题了? (它曾经有效,所以我怀疑它......)
【问题讨论】:
-
你能改写你的问题吗?你的标题不清楚。你的第一段有一些噪音。第一个 sn-p 的格式/缩进关闭。从 Xcode 复制/过去,然后选择所有代码并执行
cmmd + K。 -
@Honey 希望这现在更清楚了,我清理了问题和代码。谢谢。
-
那么当您点击按钮时实际会发生什么?
-
@matt 什么都没有。它以前只是把我推到苹果地图,现在我点击它并没有任何反应。
-
您是否设置了断点以查看您的代码是否被调用?
标签: ios swift xcode button mapkit