Swift - 将MKAnnotationView添加到MKMapView

 岁月完好 发布于 2023-01-07 11:29

我想加入MKAnnotationView,MKMapView但我不能这样做......任何人都可以帮助我吗?

这是我的代码:

override func viewDidLoad() {
    super.viewDidLoad()
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.startUpdatingLocation()
    var latitude:CLLocationDegrees = locationManager.location.coordinate.latitude
    var longitude:CLLocationDegrees = locationManager.location.coordinate.longitude
    var homeLati: CLLocationDegrees = 40.01540192
    var homeLong: CLLocationDegrees = 20.87901079
    var latDelta:CLLocationDegrees = 0.01
    var longDelta:CLLocationDegrees = 0.01
    var theSpan:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
    var myHome:CLLocationCoordinate2D = CLLocationCoordinate2DMake(homeLati, homeLong)
    var myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
    var theRegion:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, theSpan)
    self.theMapView.setRegion(theRegion, animated: true)
    self.theMapView.mapType = MKMapType.Hybrid
    self.theMapView.showsUserLocation = true
    ///Red Pin
    var myHomePin = MKPointAnnotation()
    myHomePin.coordinate = myHome
    myHomePin.title = "Home"
    myHomePin.subtitle = "Bogdan's home"
    self.theMapView.addAnnotation(myHomePin)

    var anView:MKAnnotationView = MKAnnotationView()
    anView.annotation = myHomePin
    anView.image = UIImage(named:"xaxas")
    anView.canShowCallout = true
    anView.enabled = true
}

小智.. 37

您需要实现viewForAnnotation委托方法并MKAnnotationView从那里返回一个.

这是一个例子:

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
    if (annotation is MKUserLocation) {
        //if annotation is not an MKPointAnnotation (eg. MKUserLocation),
        //return nil so map draws default view for it (eg. blue dot)...
        return nil
    }

    let reuseId = "test"

    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
    if anView == nil {
        anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        anView.image = UIImage(named:"xaxas")
        anView.canShowCallout = true
    }
    else {
        //we are re-using a view, update its annotation reference...
        anView.annotation = annotation
    }

    return anView
}

请记住,当您想要使用自己的自定义图像时,需要创建一个平面 MKAnnotationView.本MKPinAnnotationView应只用于标准引脚注解.

另请注意,您不应该locationManager.location在致电后立即尝试访问startUpdatingLocation.它可能尚未准备好.

使用didUpdateLocations委托方法.

您还需要调用requestAlwaysAuthorization/ requestWhenInUseAuthorization(请参阅位置服务在iOS 8中不起作用).

1 个回答
  • 您需要实现viewForAnnotation委托方法并MKAnnotationView从那里返回一个.

    这是一个例子:

    func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
        if (annotation is MKUserLocation) {
            //if annotation is not an MKPointAnnotation (eg. MKUserLocation),
            //return nil so map draws default view for it (eg. blue dot)...
            return nil
        }
    
        let reuseId = "test"
    
        var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
        if anView == nil {
            anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            anView.image = UIImage(named:"xaxas")
            anView.canShowCallout = true
        }
        else {
            //we are re-using a view, update its annotation reference...
            anView.annotation = annotation
        }
    
        return anView
    }
    

    请记住,当您想要使用自己的自定义图像时,需要创建一个平面 MKAnnotationView.本MKPinAnnotationView应只用于标准引脚注解.

    另请注意,您不应该locationManager.location在致电后立即尝试访问startUpdatingLocation.它可能尚未准备好.

    使用didUpdateLocations委托方法.

    您还需要调用requestAlwaysAuthorization/ requestWhenInUseAuthorization(请参阅位置服务在iOS 8中不起作用).

    2023-01-07 11:32 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有