Swift adding annotations on map

前端 未结 2 890
别那么骄傲
别那么骄傲 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:36

    The function addAnnotation takes an array of type CLLocation, which is what you should use. So make the array look like this to initialize CLLocation objects. I assume you already have a working rendered map, mapView object, etc.

    let coords = [  CLLocation(latitude: xxxx, longitude: xxxx),
       CLLocation(latitude: xxx, longitude: xxx),
       CLLocation(latitude: xxx, longitude:xxx)
        ];
    

    here is a function that can take that array, and loops through each element and adds it as an annotation to the mapView (not yet rendered)

    func addAnnotations(coords: [CLLocation]){
            for coord in coords{
                let CLLCoordType = CLLocationCoordinate2D(latitude: coord.coordinate.latitude,
                                                          longitude: coord.coordinate.longitude);
                let anno = MKPointAnnotation();
                anno.coordinate = CLLCoordType;
                mapView.addAnnotation(anno);
            }
    
        }
    

    Finally, you need to use the MKMapViewDelegate delegate to use one of its automatically called methods, when a new annotation is added, so we can queue and render it. This would be unnecessary if you were adding annotations once, but it is good to have them added like this so you can manipulate them later.

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        if annotation is MKUserLocation{
            return nil;
        }else{
            let pinIdent = "Pin";
            var pinView: MKPinAnnotationView;
            if let dequeuedView = mapView.dequeueReusableAnnotationViewWithIdentifier(pinIdent) as? MKPinAnnotationView {
                dequeuedView.annotation = annotation;
                pinView = dequeuedView;
            }else{
                pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: pinIdent);
    
            }
            return pinView;
        }
    }
    

    Do not just add this code and expect it to work, make sure you incorporate it correctly, in the right areas of your project. Do comment if you have further questions.

    UPDATE:

    remember to inherit the MKMapViewDelegate protocol into the controller your using.

    Make sure you actually call addAnnotations, in ViewDidLoad, and pass through coords array. Which can be defined in ViewDidLoad.

    Make sure the mapView method is not in ViewDidLoad but a member of the controller.

    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
提交回复
热议问题