【问题标题】:How to generate different pins on mapview in Swift 3?如何在 Swift 3 中的 mapview 上生成不同的图钉?
【发布时间】:2018-02-08 21:53:33
【问题描述】:

我遇到了一个问题,我尝试生成不同的图标以显示在地点的地图视图上。但是,我需要你们的帮助。到目前为止,我已经硬编码了一个 pin 以显示在地图视图上。我的资产中也有不同的引脚,我想通过在地图视图上生成来显示它们。如何从 API 生成不同的图标以显示在我的地图视图上?谢谢您的帮助。

这是我的代码:

import UIKit
import MapKit
import CoreLocation

class MapViewController: BaseViewController{
    @IBOutlet weak var leadingConstraints: NSLayoutConstraint!
    @IBOutlet weak var menuView: UIView!
    @IBOutlet weak var mapView: MKMapView!

    fileprivate let locationManager = CLLocationManager()
    fileprivate var startedLoadingPOIs = false
    fileprivate var places = [Place]()
    fileprivate var arViewController: ARViewController!
    @IBOutlet weak var activityIndicator: UIActivityIndicatorView!

    var nearMeIndexSelected = NearMeIndexTitle()
    var menuShowing = false

    override func viewDidLoad() {
        super.viewDidLoad()
        mapView.delegate = self

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.startUpdatingLocation()
        locationManager.requestWhenInUseAuthorization()

        //making shadow of our menu view
        menuView.layer.shadowOpacity = 1
        menuView.layer.shadowRadius = 7
    }

    @IBAction func showARController(_ sender: Any) {
        arViewController = ARViewController()
        arViewController.dataSource = self
        arViewController.maxVisibleAnnotations = 30
        arViewController.headingSmoothingFactor = 0.05
        arViewController.setAnnotations(places)
        self.navigationController!.pushViewController(arViewController, animated: true)
    }
}

extension MapViewController: CLLocationManagerDelegate, MKMapViewDelegate {
    //  // Changing the Pin Color on the map.
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

        if annotation is MKUserLocation {
            mapView.tintColor = #colorLiteral(red: 0.8823529412, green: 0.1647058824, blue: 0.1333333333, alpha: 1)
            return nil
        } else {
            let annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin")
            let pin = mapView.view(for: annotation) ?? MKAnnotationView(annotation: annotation, reuseIdentifier: nil)
            pin.image = UIImage(named: "pins")

            return pin

            return annotationView
        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if locations.count > 0 {
            let location = locations.last!
            print("Accuracy: \(location.horizontalAccuracy)")
            if location.horizontalAccuracy < 100 {
                manager.stopUpdatingLocation()
                let span = MKCoordinateSpan(latitudeDelta: 0.013, longitudeDelta: 0.013)
                let region = MKCoordinateRegion(center: location.coordinate, span: span)
                mapView.region = region
                if !startedLoadingPOIs {
                    DispatchQueue.main.async {
                        self.activityIndicator.startAnimating()
                    }
                    startedLoadingPOIs = true
                    let loader = PlacesLoader()
                    loader.loadPOIS(location: location, radius: 1500) { placesDict, error in
                        if let dict = placesDict {
                            guard let placesArray = dict.object(forKey: "results") as? [NSDictionary] else { return }
                            for placeDict in placesArray {
                                let latitude = placeDict.value(forKeyPath: "geometry.location.lat") as! CLLocationDegrees
                                let longitude = placeDict.value(forKeyPath: "geometry.location.lng") as! CLLocationDegrees
                                let reference = placeDict.object(forKey: "reference") as! String
                                let name = placeDict.object(forKey: "name") as! String
                                let address = placeDict.object(forKey: "vicinity") as! String

                                let location = CLLocation(latitude: latitude, longitude: longitude)

                                let place = Place(location: location, reference: reference, name: name, address: address)
                                self.places.append(place)
                                let annotation = PlaceAnnotation(location: place.location!.coordinate, title: place.placeName)
                                DispatchQueue.main.async {
                                    self.mapView.addAnnotation(annotation)
                                }
                            }

                            DispatchQueue.main.async {
                                self.activityIndicator.stopAnimating()
                                self.mapView.isHidden = false
                            }
                        }
                    }
                }
            }
        }
    }
}

【问题讨论】:

    标签: ios swift mapkit mkmapview


    【解决方案1】:

    您可以比较注释的坐标并为该注释指定自定义引脚。

    if annotation.coordinate == "Your Custom Pin Coordinate" { //set custom pin }
    

    假设我想为我选择的地点添加一个自定义图钉。

    var selectedPlace: PlaceAnnotation
    

    在你的循环中。假设我选择的地方是“多伦多”

    if name == "toronto" { self.selectedPlace = annotation }
    

    然后在 ViewForAnnotation 方法中

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if annotation.coordinate = selectedPlace.coordinate {
            pin.image = UIImage(named: "YOUR SELECTED IMAGE")
        }
    }
    

    【讨论】:

    • 您能否具体说明一下自定义引脚坐标的含义?
    • 假设我想为我选择的图像添加一个自定义图钉
    • 从我的资产文件夹中生成并将其放在来自地点 API 的地图视图上
    【解决方案2】:

    在这里查看我的演示以创建自定义引脚视图customPinAnnotationButton

    这里是用图片绘制注解的方法,我继承了MKAnoationView

        func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?
        { 
            if annotation is MyAnnotation == false
            {
    
                return nil
            }
    
            let senderAnnotation = annotation as! MyAnnotation
    
            let pinReusableIdentifier = senderAnnotation.pinColor.rawValue
    
            var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: pinReusableIdentifier)
    
            if annotationView == nil
            {
    
                annotationView = MKAnnotationView(annotation: senderAnnotation,                                                                                                reuseIdentifier: pinReusableIdentifier)
                annotationView!.canShowCallout = false
    
            }
    
            if senderAnnotation.pinColor == PinColor.Green
            {
    
                    let pinImage = UIImage(named:"directMarker3.png")
    
                    annotationView!.image = pinImage
    
            }
    
    
          return annotationView
    
    }
    

    在这里添加注释

        let blueLocation = CLLocationCoordinate2D(latitude:30.45454554, longitude: 29.646727)
    
        let blueAnnotation = MyAnnotation(coordinate: blueLocation, title:"ghghhg",subtitle: "hgnhhghghg",pinColor: .Green ,uid:"hghg",type:"provider")
    
        self.mymap.addAnnotation(blueAnnotation)        
    
        self.mymap.showAnnotations(self.mymap.annotations, animated: true)
    

    【讨论】:

    • 我得到了你的演示,但有点混乱。你能通过我上面的代码给我看一个sn-p吗?
    • 这不是显示一个硬编码图标吗?
    • 所以,如果我的资产文件夹中有不同的位置图标,我需要指定每个图标,就像您在添加注释时显示的那样?
    • 创建一个数组,然后向自定义类添加一个名为 index 的属性,在 viewForAnnoation 中访问它 ORRRR 创建一个全局 UIImage var 并在每个 addAnnoation 循环之前更改它
    • 听起来不错。让我试试,如果我卡住了,我会告诉你的
    【解决方案3】:

    1.定义MKPointAnnotation的子类:

    class MyPointAnnotation: MKPointAnnotation {
        var imageName: String = ""
    }
    

    2.设置图片名称。

        let annotation = MyPointAnnotation()
        annotation.coordinate = coordinate
        annotation.title = "title"
        annotation.subtitle = "subtitle"
        annotation.imageName = "pin" // Set image name here
        self.mapView.addAnnotation(annotation)
    

    3.在viewFor委托方法中加载图片

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if annotation is MKUserLocation {
            return nil
        }
    
        let reuseId = "image"
        var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
        if pinView == nil {
            pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            pinView?.canShowCallout = true
            let annotation = annotation as! MyPointAnnotation
            pinView?.image = UIImage(named: annotation.imageName)
    
            let rightButton: AnyObject! = UIButton(type: UIButtonType.detailDisclosure)
            pinView?.rightCalloutAccessoryView = rightButton as? UIView
        }
        else {
            pinView?.annotation = annotation
        }
    
        return pinView
    }
    

    【讨论】:

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