我是快速编程的新手,我正在尝试构建一个在地图上显示一些图钉的应用程序。当用户单击图钉时,将带有按钮的注释。如果单击该按钮,它将在Safari中打开一个URL。
在完成一些教程之后,我设法拥有了一个带有大头针和按钮的功能性应用程序,但是我无法打开URL。
这是我用来构建应用程序的代码:
struct WebCam {
var name: String
var latitude: CLLocationDegrees
var longitude: CLLocationDegrees
var url: String
}
class FirstViewController: UIViewController,CLLocationmanagerDelegate {
@IBOutlet weak var mapView: MKMapView!
let webcams = [WebCam(name: "WH1",latitude: 51.5549,longitude: -0.108436,url: "www.test.it"),WebCam(name: "WH2",latitude: 51.4816,longitude: -0.191034,url: "www.google.it")]
func fetchWebcamsOnmap(_ webcams: [WebCam]) {
for webcam in webcams {
let annotations = MKPointAnnotation()
annotations.title = webcam.name
annotations.coordinate = CLLocationCoordinate2D(latitude: webcam.latitude,longitude: webcam.longitude)
mapView.addAnnotation(annotations)
}
}
extension FirstViewController: mkmapviewdelegate {
func mapView(_ mapView: MKMapView,viewFor annotations: MKAnnotation) ->
MKAnnotationView? {
let identifier = "WebCam"
var view: MKMarkerAnnotationView
// 4
if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
as? MKMarkerAnnotationView {
dequeuedView.annotation = annotations
view = dequeuedView
} else {
// 5
view = MKMarkerAnnotationView(annotation: annotations,reuseIdentifier: identifier)
view.canShowCallout = true
view.calloutOffset = CGPoint(x: -5,y: 5)
let mapsButton = UIButton(frame: CGRect(origin: CGPoint.zero,size: CGSize(width: 30,height: 30)))
mapsButton.setBackgroundImage(UIImage(named: "webcam"),for: UIControl.State())
view.rightCalloutaccessoryView = mapsButton
}
return view
}
这是检索UIButton按下的代码
func mapView(_ mapView: MKMapView,annotationView view: MKAnnotationView,calloutaccessoryControlTapped control: UIControl) {
if control == view.rightCalloutaccessoryView {
}
}
我试图通过使用
访问选定网络摄像头的网址来进行简单的控制台打印let web = view.annotation as! WebCam
let webURL = WebCam.url
,但显示错误。