我向您推荐一个示例方法,在您的 pin 上添加一个按钮,并通过苹果地图应用程序向用户提供 pin 的方向。
首先,在您的 pin 声明之后,您应该声明一个按钮,让您有机会与 pin 中的用户交互(我们稍后将展示如何初始化 pin 中的按钮)。
你可以这样声明你的按钮:
let smallSquare = CGSize(width: 30, height: 30)
let button = UIButton(frame: CGRect(origin: CGPointZero, size: smallSquare))
注意,smallSquare 为按钮提供您未来图钉按钮的正确大小。
您可以使用button.setBackgroundImage(UIImage(named: "car"), forState: .Normal) 方法在图钉按钮中添加图片。
Apple 开发者给我们一些文档:Apple developer web site.
然后您可以使用此方法进行按钮操作:button.addTarget(self, action: #selector(ViewController.getDirections), forControlEvents: .TouchUpInside)。
所以,像这样,如果按钮被正常选中(.TouchUpInside),程序调用函数getDirection ()。
现在,您可以使用以下方法初始化 pin 中的按钮:pinView?.leftCalloutAccessoryView = button。
注意此方法将按钮设置在图钉的左侧。您可以像这样将按钮放在右侧:pinView?.rightCalloutAccessoryView = button。
Apple developer提供一些关于这个主题的信息。
但是,我不知道如何在引脚的整个表面初始化按钮。
最后,你应该有函数getDirections ()给用户引脚的“方向”。
我建议您为此使用苹果地图应用程序。
我的函数getDirections () 如下所示:
func getDirections(){
guard let selectedPin = selectedPin else { return }
let mapItem = MKMapItem(placemark: selectedPin)
let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
mapItem.openInMapsWithLaunchOptions(launchOptions)
print ("GET DIRECTION")
}
我在这个web site 中找到了这个功能。我认为它可以比我更好地解释这一点。
最后我的程序是这样的:
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?{
guard !(annotation is MKUserLocation) else { return nil }
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
}
pinView?.pinTintColor = UIColor.orangeColor()
pinView?.canShowCallout = true
//The initialization of the pin. Here your program looks great.
// And after the code as seen above.
let smallSquare = CGSize(width: 30, height: 30)
let button = UIButton(frame: CGRect(origin: CGPointZero, size: smallSquare))
button.setBackgroundImage(UIImage(named: "car"), forState: .Normal)
button.addTarget(self, action: #selector(ViewController.getDirections), forControlEvents: .TouchUpInside)
pinView?.leftCalloutAccessoryView = button
return pinView
}
func getDirections(){
guard let selectedPin = selectedPin else { return }
let mapItem = MKMapItem(placemark: selectedPin)
let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
mapItem.openInMapsWithLaunchOptions(launchOptions)
print ("GET DIRECTION")
}
通常,您的程序应如下所示:
func addPinOnMap(_ address: String) {
var hotelClass = PFObject(className: HOTEL_CLASS_NAME)
hotelClass = hotelArray[0]
if mapView.annotations.count != 0 {
annotation = mapView.annotations[0]
mapView.removeAnnotation(annotation)
}
// Make a search on the Map
localSearchRequest = MKLocalSearchRequest()
localSearchRequest.naturalLanguageQuery = address
localSearch = MKLocalSearch(request: localSearchRequest)
localSearch.start { (localSearchResponse, error) -> Void in
// Add PointAnnonation text and a Pin to the Map
self.pointAnnotation = MKPointAnnotation()
self.pointAnnotation.title = "\(hotelClass[HOTEL_NAME]!)"
self.pointAnnotation.coordinate = CLLocationCoordinate2D( latitude: localSearchResponse!.boundingRegion.center.latitude, longitude:localSearchResponse!.boundingRegion.center.longitude)
self.pinView = MKPinAnnotationView(annotation: self.pointAnnotation, reuseIdentifier: nil)
self.mapView.centerCoordinate = self.pointAnnotation.coordinate
self.mapView.addAnnotation(self.pinView.annotation!)
let smallSquare = CGSize(width: 30, height: 30)
let button = UIButton(frame: CGRect(origin: CGPointZero, size: smallSquare))
button.setBackgroundImage(UIImage(named: "car"), forState: .Normal)
button.addTarget(self, action: #selector(ViewController.getDirections), forControlEvents: .TouchUpInside)
self.pinView?.leftCalloutAccessoryView = button
// Zoom the Map to the location
self.region = MKCoordinateRegionMakeWithDistance(self.pointAnnotation.coordinate, 1000, 1000);
self.mapView.setRegion(self.region, animated: true)
self.mapView.regionThatFits(self.region)
self.mapView.reloadInputViews()
}
func getDirections(){
guard let selectedPin = selectedPin else { return }
let mapItem = MKMapItem(placemark: selectedPin)
let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
mapItem.openInMapsWithLaunchOptions(launchOptions)
print ("GET DIRECTION")
}
对不起,我不能尝试你的代码,因为我没有像 pinView 声明那样的完整代码。
注意,您需要添加这行代码才能接受,而不是 pin 可以显示标注pinView?.canShowCallout = true。
如果您想了解更多详细信息或告诉我评论,可以访问此网站。
Thorn technologies
Apple developer