我正在尝试在Swift中使用MapKit。我需要显示当前用户位置和其旁边的兴趣点的 map 区域,但是这些兴趣点必须具有不同的可视化用户本身模式。我知道有必要实现MapViewAnnotation的委托,将其放入我的代码中不会运行。有人可以帮我举个例子吗?

这是我的代码。

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate {

    @IBOutlet weak var mapView: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()


        let latArray = [-23.528657, -23.518514, -23.533796, -23.533796]
        let longArray = [-46.484008, -46.486495, -46.495533, -46.476690]

        var lat: CLLocationDegrees = -23.527096772791133
        var long: CLLocationDegrees = -46.48964569157911

        var latDelta: CLLocationDegrees = 0.01
        var longDelta: CLLocationDegrees = 0.01

        var theSpan: MKCoordinateSpan = MKCoordinateSpanMake(latDelta,longDelta)

        var mypos: CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat,long)

        var myreg: MKCoordinateRegion = MKCoordinateRegionMake(mypos, theSpan)

        self.mapView.setRegion(myreg, animated: true)

        var myposannot = MKPointAnnotation()

        myposannot.coordinate = mypos
        myposannot.title = "Me"
        myposannot.subtitle = "I am here!"

        self.mapView.addAnnotation(myposannot)

        for var i=0; i<4; ++i {

            var latCli: CLLocationDegrees = latArray[i]
            var longCli: CLLocationDegrees = longArray[i]

            var myposCli : CLLocationCoordinate2D = CLLocationCoordinate2DMake(latCli,longCli)

            var myposannotCli = MKPointAnnotation()

            myposannotCli.coordinate = myposCli
            myposannotCli.title = "Cliente" + " - " + String (i)
            myposannotCli.subtitle = "Anotacao" + " - " + String (i)


            self.mapView.addAnnotation(myposannotCli)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func mapViewAnnot(mapViewAnnot: MKMapView!,ViewForAnnotation annotation: MKAnnotation!) ->MKAnnotationView{

        if annotation is MKUserLocation{
            return nil
        }

        let reuseId = "pin"

        var pinView = mapViewAnnot.dequeueReusableAnnotationViewWithIdentifier(reuseId)   as? MKPinAnnotationView

        if(pinView == nil){
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            pinView!.canShowCallout = true
            pinView!.animatesDrop = true
            pinView!.pinColor = .Red
            pinView!.rightCalloutAccessoryView = UIButton.buttonWithType(.DetailDisclosure) as UIButton
        } else {
            pinView!.annotation = annotation
        }
        return pinView!
    }

}

最佳答案

该代码有两个主要问题:

  • viewForAnnotation委托方法的名称不正确,因此 map 视图将不会调用它。当前的方法声明是:
    func mapViewAnnot(mapViewAnnot: MKMapView!,
        ViewForAnnotation annotation: MKAnnotation!) ->MKAnnotationView {
    

    是错的。应该是这样的:
    func mapView(mapView: MKMapView!,
        viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
    

    该方法必须命名为mapView(mapView:viewForAnnotation:)
  • 另一个问题是这一行:
    var pinView = mapViewAnnot.dequeueReusableAnnotationViewWithIdentifier(reuseId)   as? MKPinAnnotationView
    

    对某些名为mapViewAnnot的对象的引用是没有意义的,并且必须阻止该代码进行编译。这行应该是这样的:
    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
    
  • 10-07 19:55