【问题标题】:Swift MapKit custom current location markerSwift MapKit 自定义当前位置标记
【发布时间】:2016-08-19 19:44:43
【问题描述】:

这就是我的project 目前的样子。我的问题是,如何将蓝球(当前位置)更改为自定义图像图标

【问题讨论】:

    标签: ios swift location mapkit


    【解决方案1】:

    我相信您知道用户习惯于将该蓝点视为当前用户的位置。除非有充分的理由,否则不应更改它。

    这里是如何改变它:

    为mapView设置delegate,然后重写下面的函数……类似这样:

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if annotation is MKUserLocation {
            let pin = mapView.view(for: annotation) as? MKPinAnnotationView ?? MKPinAnnotationView(annotation: annotation, reuseIdentifier: nil)
            pin.pinTintColor = UIColor.purple
            return pin
    
        } else {
            // handle other annotations
    
        }
        return nil
    }
    

    并改为显示图像: 只需将 if 语句中的代码替换为以下代码即可:

    let pin = mapView.view(for: annotation) as? MKPinAnnotationView ?? MKPinAnnotationView(annotation: annotation, reuseIdentifier: nil)
    pin.image = UIImage(named: "user_location_pin")
    return pin
    

    我认为这个代码示例应该为您提供足够的信息来帮助您弄清楚该怎么做。 (请注意,mapView 是在情节提要中创建的...)

    import UIKit
    import MapKit
    import CoreLocation
    
    class ViewController: UIViewController, MKMapViewDelegate {
        @IBOutlet weak var mapView: MKMapView!
    
        let loc = CLLocationManager()
        var angle = 0
        var timer: NSTimer!
        var userPinView: MKAnnotationView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            mapView.delegate = self
            loc.requestWhenInUseAuthorization()
    
            timer = NSTimer.scheduledTimerWithTimeInterval(0.3, target: self, selector: #selector(rotateMe), userInfo: nil, repeats: true)
        }
    
        func rotateMe() {
            angle = angle + 10
            userPinView?.transform = CGAffineTransformMakeRotation( CGFloat( (Double(angle) / 360.0) * M_PI ) )
    
        }
    
        func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
            if annotation is MKUserLocation {
                let pin = mapView.viewForAnnotation(annotation) ?? MKAnnotationView(annotation: annotation, reuseIdentifier: nil)
                pin.image = UIImage(named: "userPinImage")
                userPinView = pin
                return pin
    
            } else {
                // handle other annotations
    
            }
            return nil
        }
    }
    

    【讨论】:

    • 确实如此,非常感谢您提供干净且做得很好的代码。但是有什么办法可以把它从一个大头针改成icon
    • 谢谢,完美运行!看看here btw,你知道如何改变图像的方向吗?
    【解决方案2】:

    您可以使用 MKMapDelegate 的方法自定义视图:

    optional func mapView(_ mapView: MKMapView,
        viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?
    

    参数

    mapView - 请求注解视图的地图视图。

    annotation - 表示即将显示的注释的对象。除了您的自定义注释之外,此对象 可以是代表用户当前的 MKUserLocation 对象 位置。

    查看完整文档here

    还请参阅以下 SO 问题以在用户位置更改时更新视图: Custom Annotation view for userlocation not moving the mapview

    【讨论】:

      猜你喜欢
      • 2013-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多