Swift:向pin注释添加方向按钮



我有一个应用程序,可以使用MKMapView显示公司位置。当用户访问联系人页面时,屏幕顶部会出现一张地图,上面有一个别针,上面显示了我们的位置。单击地图接点时,它会在注释中显示公司名称。

我正在使用back4app从我的Parse后端存储的数据中将公司地址拉入MKMApView。

我让这一切都完美地工作了,然而,当我点击地图钉,它显示公司名称时,我正在尝试在那里设置一个汽车图标按钮,这样用户就可以获得前往我们公司的路线。

以下是我必须设置注释的代码:

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!)
// 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()
}
}

我只是不知道如何在注释中获得按钮图标,并在单击按钮时显示方向。无论是使用当前地图视图,还是在用户的设备上打开地图应用程序,都可以。

谢谢。

我建议您使用一个示例方法,在您的引脚上设置一个按钮,并使用苹果地图应用程序向用户提供引脚的方向。

首先,在你的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)

苹果开发者给我们一些文档:苹果开发者网站。


然后可以使用此方法进行按钮操作:button.addTarget(self, action: #selector(ViewController.getDirections), forControlEvents: .TouchUpInside)。因此,像这样,如果按钮被正常选择(.TouchUpInside),程序将调用函数getDirection ()


现在,您可以使用以下方法初始化引脚中的按钮:pinView?.leftCalloutAccessoryView = button

注意此方法设置引脚左侧的按钮。你可以像这样把按钮放在右边:pinView?.rightCalloutAccessoryView = button

苹果开发者提供了一些关于这个主题的信息。

然而,我不知道如何在引脚的整个表面初始化按钮。


最后,您应该具有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")
}

我在这个网站上找到了这个功能。我认为它可以比我更好地解释这一点。

最后,我的程序看起来是这样的:

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声明那样完全的代码。

注意,您可能需要添加这行代码才能接受,而引脚可以显示标注pinView?.canShowCallout = true

如果你想了解更多细节或给我一个评论,你可以查阅这些网站。

Thorn技术

苹果开发者

最新更新