如何从每个注释视图创建自定义标注按钮?
how to create a custom callout button from each annotation view?
我正在尝试在每个注释视图中创建一个按钮,以将用户带到一个新页面自定义所选注释。我已经成功地实现了 mapview 以显示带有从我的解析数据库加载的标题和副标题的注释,但我正在努力寻找一种方法 1) 向注释视图添加一个按钮以将用户带到新视图 2) 找到一种方法选择时为每个注释创建自定义视图。任何帮助将不胜感激?
代码
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
var MapViewLocationManager:CLLocationManager! = CLLocationManager()
let btn = UIButton(type: .DetailDisclosure)
override func viewDidLoad() {
super.viewDidLoad()
mapView.showsUserLocation = true
mapView.delegate = self
MapViewLocationManager.delegate = self
MapViewLocationManager.startUpdatingLocation()
mapView.setUserTrackingMode(MKUserTrackingMode.Follow, animated: true)
}
override func viewDidAppear(animated: Bool) {
let annotationQuery = PFQuery(className: "Clubs")
annotationQuery.findObjectsInBackgroundWithBlock {
(clubs, error) -> Void in
if error == nil {
// The find succeeded.
print("Successful query for annotations")
// Do something with the found objects
let myClubs = clubs! as [PFObject]
for club in myClubs {
//data for annotation
let annotation = MKPointAnnotation()
let place = club["location"] as? PFGeoPoint
let clubName = club["clubName"] as? String
let stadiumName = club["stadium"] as? String
annotation.title = clubName
annotation.subtitle = stadiumName
annotation.coordinate = CLLocationCoordinate2DMake(place!.latitude,place!.longitude)
//add annotations
self.mapView.addAnnotation(annotation)
}
} else {
// Log details of the failure
print("Error: \(error)")
}
}
}
这是Objective-C解决方案,希望对您有所帮助。
将 button
设置为 annotationView
、
UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annotationView.rightCalloutAccessoryView = detailButton;
当click
在这个按钮上执行时,下面的方法像delegate
一样被调用,所以也重写下面的方法,
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
您可以在上述方法中进行任何自定义,因为您知道用户现在点击了附件视图。
对于第一个 - 您可以在 annoationView 上使用 leftCalloutAccessoryView 和 rightCalloutAccessoryView。您将必须实施 MapViewDelegate
mapView(_:viewForAnnotation:).
对于第二个 - 与 calloutAccessoryView 类似,您可以访问 annoationView 上的 detailCalloutAccessoryView。您可以使用它来自定义标注。
我正在尝试在每个注释视图中创建一个按钮,以将用户带到一个新页面自定义所选注释。我已经成功地实现了 mapview 以显示带有从我的解析数据库加载的标题和副标题的注释,但我正在努力寻找一种方法 1) 向注释视图添加一个按钮以将用户带到新视图 2) 找到一种方法选择时为每个注释创建自定义视图。任何帮助将不胜感激?
代码
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
var MapViewLocationManager:CLLocationManager! = CLLocationManager()
let btn = UIButton(type: .DetailDisclosure)
override func viewDidLoad() {
super.viewDidLoad()
mapView.showsUserLocation = true
mapView.delegate = self
MapViewLocationManager.delegate = self
MapViewLocationManager.startUpdatingLocation()
mapView.setUserTrackingMode(MKUserTrackingMode.Follow, animated: true)
}
override func viewDidAppear(animated: Bool) {
let annotationQuery = PFQuery(className: "Clubs")
annotationQuery.findObjectsInBackgroundWithBlock {
(clubs, error) -> Void in
if error == nil {
// The find succeeded.
print("Successful query for annotations")
// Do something with the found objects
let myClubs = clubs! as [PFObject]
for club in myClubs {
//data for annotation
let annotation = MKPointAnnotation()
let place = club["location"] as? PFGeoPoint
let clubName = club["clubName"] as? String
let stadiumName = club["stadium"] as? String
annotation.title = clubName
annotation.subtitle = stadiumName
annotation.coordinate = CLLocationCoordinate2DMake(place!.latitude,place!.longitude)
//add annotations
self.mapView.addAnnotation(annotation)
}
} else {
// Log details of the failure
print("Error: \(error)")
}
}
}
这是Objective-C解决方案,希望对您有所帮助。
将 button
设置为 annotationView
、
UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annotationView.rightCalloutAccessoryView = detailButton;
当click
在这个按钮上执行时,下面的方法像delegate
一样被调用,所以也重写下面的方法,
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
您可以在上述方法中进行任何自定义,因为您知道用户现在点击了附件视图。
对于第一个 - 您可以在 annoationView 上使用 leftCalloutAccessoryView 和 rightCalloutAccessoryView。您将必须实施 MapViewDelegate mapView(_:viewForAnnotation:).
对于第二个 - 与 calloutAccessoryView 类似,您可以访问 annoationView 上的 detailCalloutAccessoryView。您可以使用它来自定义标注。