Swift:将方向按钮添加到固定注释
Swift: Add Directions button to pin annotation
我有一个使用 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)
。
苹果开发者给了我们一些文档: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 ()
给用户 "direction" 的 pin。
我建议您为此目的使用苹果地图应用程序。
我的函数 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
。
如果您需要更多详细信息或告诉我评论,您可以查阅该网站。
我有一个使用 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)
。
苹果开发者给了我们一些文档: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 ()
给用户 "direction" 的 pin。
我建议您为此目的使用苹果地图应用程序。
我的函数 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
。
如果您需要更多详细信息或告诉我评论,您可以查阅该网站。