我试图做一个别针,然后单击它,标题和副标题就会出现。
到目前为止,这是我的代码:
let span : MKCoordinateSpan = MKCoordinateSpanMake(0.1, 0.1)
let location : CLLocationCoordinate2D = CLLocationCoordinate2DMake(40.750517, -73.987271)
let region : MKCoordinateRegion = MKCoordinateRegionMake(location, span)
mapView.setRegion(region, animated: true)
let annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = "Bus Stop"
annotation.subtitle = "Street Name at Street Name"
mapView.addAnnotation(annotation)
问题是我的气泡不是我想要的,它是我想要的,我想要在应用程序中使用的那些传统的红色别针,当您单击更多信息时会显示这些红色的别针。
最佳答案
扩展YourViewC:MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation
{
return nil
}
var annotationView = self.mapView.dequeueReusableAnnotationView(withIdentifier: "CustomPin")
if annotationView == nil{
annotationView = AnnotationView(annotation: annotation, reuseIdentifier: "CustomPin")
annotationView?.canShowCallout = false
}else{
annotationView?.annotation = annotation
}
annotationView?.image = UIImage(named: "bluecircle") // Pass name of your custom image
return annotationView
}
func mapView(_ mapView: MKMapView,
didSelect view: MKAnnotationView){
let annotation = view.annotation as? CustomAnnotation
print(annotation?.address) // get info you passed on pin
// write code here to add custom view on tapped annotion
}
关于swift - 将注释更改为图钉,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51537295/