swift MKPointAnnotation自定义图像

3b6akqbq  于 2023-10-15  发布在  Swift
关注(0)|答案(6)|浏览(145)

这是我的代码,我想添加一个自定义引脚(.png文件),而不是红色引脚。我尝试使用MKPinAnnotationViewMKAnnotationView,但我不能添加坐标,字幕和标题。我是iOS开发新手。

override func viewDidLoad() {
    super.viewDidLoad()
    // Handle the text field’s user input through delegate callbacks.
    commentTextField.delegate = self
    
    coreLocationManager.delegate = self
    //desired accuracy is the best accuracy, very accurate data for the location
    coreLocationManager.desiredAccuracy = kCLLocationAccuracyBest
    //request authorization from the user when user using my app
    coreLocationManager.requestWhenInUseAuthorization()
    
    coreLocationManager.startUpdatingLocation()
    
    dbRef = FIRDatabase.database().reference()
    
    struct Location {
        let title: String
        let latitude: Double
        let longitude: Double
        let subtitle: String
    }
    // Locations array
    let locations = [
        Location(title: "Dio Con Dio",    latitude: 40.590130, longitude: 23.036610,subtitle: "cafe"),
        Location(title: "Paradosiako - Panorama", latitude: 40.590102, longitude: 23.036180,subtitle: "cafe"),
        Location(title: "Veranda",     latitude: 40.607740, longitude: 23.103044,subtitle: "cafe")
    ]
    
    for location in locations {
        let annotation = MKPointAnnotation()
        
        annotation.title = location.title
        annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
        annotation.subtitle = location.subtitle
        
        map.addAnnotation(annotation)
    }
}

使用Swift 3

ioekq8ef

ioekq8ef1#

FWIW,现在我一般不会实现mapView(_:viewFor:)。我只需要在viewDidLoad中注册我的注解视图:

mapView.register(DogAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)

我会有一个annotation view子类,它会适当地配置annotation view:

class DogAnnotationView: MKAnnotationView {
    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        configure(for: annotation)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        configure(for: annotation)
    }

    override var annotation: MKAnnotation? { didSet { configure(for: annotation) } }

    private func configure(for annotation: MKAnnotation?) {
        image = UIImage(systemName: "dog.fill")
        …
    }
}

屈服:

或者我们可以使用MKMarkerAnnotationView

class DogAnnotationView: MKMarkerAnnotationView {
    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        configure(for: annotation)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        configure(for: annotation)
    }

    override var annotation: MKAnnotation? { didSet { configure(for: annotation) } }

    private func configure(for annotation: MKAnnotation?) {
        glyphImage = UIImage(systemName: "dog.fill")
        …
    }
}

屈服:

无论哪种方式,您都可以对注解视图进行任何配置(例如,我通常通过设置clusteringIdentifier来使用集群,并为MKMapViewDefaultClusterAnnotationViewReuseIdentifier注册另一个注解视图类),但这与此无关。这个想法是注册注解视图类并将其配置放在注解视图类中,而不是使用MKMapViewDelegate方法mapView(_:viewFor:)导致视图控制器膨胀。
为了子孙后代的缘故,我最初的mapView(_:viewFor:)答案如下。
您需要指定您的视图控制器作为Map视图的委托(在IB中或在viewDidLoad中以编程方式指定),然后(a)指定您符合MKMapViewDelegate协议;以及(B)实现mapView(_:viewFor:)

extension ViewController: MKMapViewDelegate {
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        let identifier = "MyPin"
        
        if annotation is MKUserLocation {
            return nil
        }
        
        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
        
        if annotationView == nil {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            annotationView?.canShowCallout = true
            annotationView?.image = UIImage(named: "custom_pin.png")
            
            // if you want a disclosure button, you'd might do something like:
            //
            // let detailButton = UIButton(type: .detailDisclosure)
            // annotationView?.rightCalloutAccessoryView = detailButton
        } else {
            annotationView?.annotation = annotation
        }
        
        return annotationView
    }
}

有关更多信息,请参阅位置和Map编程指南:从代理对象创建注解视图。代码片段是用C2C-C编写的,但它描述了基本的过程。

q0qdq0h2

q0qdq0h22#

最后,我这样做了,靠我自己!

ViewController.swift

//
    //  ViewController.swift

    //
    //  Created by Alexandros Andreadis on 19/04/2017.
    //  Copyright © 2017 Alexandros Andreadis. All rights reserved.
    //    
    import UIKit
    import MapKit
    import CoreLocation
    import FirebaseDatabase

    class RateViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, CLLocationManagerDelegate, MKMapViewDelegate{
    let pin = UIImage(named: "pin")
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?
        {

            if let annotation = annotation as? Locations{
                if let view = mapView.dequeueReusableAnnotationView(withIdentifier: annotation.identifier){
                    return view
                }else{
                    let view = MKAnnotationView(annotation: annotation, reuseIdentifier: annotation.identifier)
                    view.image = pin
                    view.isEnabled = true
                    view.canShowCallout = true
                    //view.leftCalloutAccessoryView = UIImageView(image: pin)
                    return view
                }
            }
            return nil
        }
        override func viewDidLoad() {
            super.viewDidLoad()

            mapView.delegate = self 

            mapView.addAnnotations(locations)


        }

    }

地点.swift

//
//  Locations.swift

//
//  Created by Alexandros Andreadis on 10/05/2017.
//  Copyright © 2017 Alexandros Andreadis. All rights reserved.
//

import UIKit
import MapKit

class Locations: NSObject, MKAnnotation {
    // required coordinate, title, and the reuse identifier for this annotation
    var identifier = "locations"
    var title: String?
    var coordinate: CLLocationCoordinate2D
    //initializer taking a name, a latitude and longitude to populate the title and coordinate for each instance of this object
    init(name:String,lat:CLLocationDegrees,long:CLLocationDegrees){
        title = name
        coordinate = CLLocationCoordinate2DMake(lat, long)
    }

}
// Creating the list of the places that will be pinned in map
class LocationList: NSObject {
    var Location = [Locations]()
    override init(){
        Location += [Locations(name: "Dio Con Dio", lat: 40.590130, long: 23.036610)]
        Location += [Locations(name: "Paradosiako - Panorama", lat: 40.590102, long:23.036180)]
        Location += [Locations(name: "Veranda",  lat: 40.607740, long: 23.103044)]
        Location += [Locations(name: "Markiz",  lat: 40.634252, long: 22.936276)]
        Location += [Locations(name: "Moi Lounge Bar",  lat: 40.653481, long: 22.994131)]
        Location += [Locations(name: "Boulevard Lounge Bar",  lat: 40.658462, long: 22.983198)]
        Location += [Locations(name: "Ernést Hébrard",  lat: 40.631829, long: 22.941014)]
        Location += [Locations(name: "Tribeca - All Day & Night Bar",  lat: 40.631029, long: 22.942396)]

    }
}
vdgimpew

vdgimpew3#

SWIFT 5

let pointAnnotation = MKPointAnnotation()

override func viewDidLoad()
{
   mapVw.delegate = self
 pointAnnotation.coordinate = CLLocationCoordinate2D(latitude: yourLatitude, longitude:yourLongitude)
 mapVw.addAnnotation(pointAnnotation)

}

// MARK:- MapView Delegate

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?
{
    if annotation is MKUserLocation
    {
        return nil;
    }else{
        let pinIdent = "Pin";
        var pinView: MKAnnotationView?
        if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: pinIdent)
        {
            dequeuedView.annotation = annotation;
            pinView = dequeuedView;
        }
        else
        {
            pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: pinIdent);
            pinView?.image = UIImage(named: "yourImage")

        }

        return pinView;
    }
}
snz8szmq

snz8szmq4#

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

    let identifier = "MyPin"

    if annotation.isKindOfClass(MKUserLocation) {
        return nil
    }

    let detailButton: UIButton = UIButton(type: UIButtonType.DetailDisclosure)

    if let annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")
        annotationView.canShowCallout = true
        annotationView.image = UIImage(named: "custom_pin.png")
        annotationView.rightCalloutAccessoryView = detailButton
    }
    else {
        annotationView.annotation = annotation
    }

    return annotationView
}
esbemjvw

esbemjvw5#

我是这样补充的:

func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {
  let sourceView = views.first
  sourceView?.image = UIImage(named: "Bus")
  let destinationView = views.last
  destinationView?.image = UIImage(named: "Home")
}
xdyibdwo

xdyibdwo6#

使用此方法:

optional func mapView(_ mapView: MKMapView, 
           didAdd views: [MKAnnotationView])

对我来说很好。(我的环境:Xcode 9.4.1和iOS 11.4.1)
范例:

func mapView(_ mapView: MKMapView,
             didAdd views: [MKAnnotationView])
{
    //views[0] = just added MKAnnotationView
    let pic = UIImage(named: "abc.jpg")
    
    views[0].image = pic
    views[0].layer.cornerRadius = (views[0].frame.size.width) / 2
    views[0].clipsToBounds = true
    
    print("fafafjejowfjpeawijoefaw")
}

相关问题