user9085884
user9085884

Reputation:

How to generate different pins on mapview in Swift 3?

I am coming to a problem where I try to generate different icons to show on the map view of places. But, I need some help from you guys. So far, I have hard-coded a pin to show on the map view. I also, have different pins in my assets, I want to show them by generating it on the mapview. How can I generate different icons to show on my map view from the API? Thanks for the help.

Here is my code:

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
                            }
                        }
                    }
                }
            }
        }
    }
}

Upvotes: 0

Views: 1369

Answers (3)

Kosuke Ogawa
Kosuke Ogawa

Reputation: 7451

1.Define subclass of MKPointAnnotation:

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

2.Set image name.

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

3.Load image in viewFor delegate method

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
}

Upvotes: 0

ashish
ashish

Reputation: 361

You can compare the coordinate of the annotation and specify custom pin for that Annotation.

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

Suppose I want to add a custom pin for my selected Place.

var selectedPlace: PlaceAnnotation

Inside your loop. suppose my selected place is "toronto"

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

Then in ViewForAnnotation method

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

Upvotes: 1

Shehata Gamal
Shehata Gamal

Reputation: 100551

See here my demo to create a custom pin view customPinAnnotationButton

Here is the method to draw annotation with image , I subclassed 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

}

here to add an annotation

    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)

Upvotes: 0

Related Questions