Swift adding annotations on map

前端 未结 2 910
别那么骄傲
别那么骄傲 2021-01-02 19:18

Trying to make some annotations on the map. But can\'t get around it. the code I\'m using is

 // Names
 names = [\"Ben\", \"Big\", \"Hawk\", \"Enot\", \"Wilt         


        
2条回答
  •  感动是毒
    2021-01-02 19:50

    Add Simple annotation with title Swift 5.0

        let addAnotation = MKPointAnnotation()
        addAnotation.title = "YOUR TITLE"
        addAnotation.coordinate = CLLocationCoordinate2D(latitude: [YOUR LATITIUDE], longitude: [YOUR LONGITUDE])
        self.mapView.addAnnotation(addAnotation)
    

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        
        guard annotation is MKPointAnnotation else { return nil }
    
        let identifier = "Annotation"
        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
    
        if annotationView == nil {
            annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            annotationView!.canShowCallout = true
        } else {
            annotationView!.annotation = annotation
        }
    
        return annotationView
    }
    

提交回复
热议问题