1

I hope my question is clear, I am beginner in apple map kit

how to insert UIbutton instate of location pins

I have this code:

witch is shows a regular pin shape.

 // maping 2


        var fromLatD:Double = Double(fromLat)!
        var fromLngD:Double = Double(fromLng)!
        var toLatD:Double = Double(toLat)!
        var toLngD:Double = Double(toLng)!


        // 1.
        mapView.delegate = self

        // 2.
        let sourceLocation = CLLocationCoordinate2D(latitude: fromLatD, longitude: fromLngD)
        let destinationLocation = CLLocationCoordinate2D(latitude: toLatD, longitude: toLngD)

        // 3.
        let sourcePlacemark = MKPlacemark(coordinate: sourceLocation, addressDictionary: nil)
        let destinationPlacemark = MKPlacemark(coordinate: destinationLocation, addressDictionary: nil)

        // 4.
        let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
        let destinationMapItem = MKMapItem(placemark: destinationPlacemark)


        // 5.
        let sourceAnnotation = MKPointAnnotation()
        sourceAnnotation.title = fromname

        if let location = sourcePlacemark.location {
            sourceAnnotation.coordinate = location.coordinate
        }


        let destinationAnnotation = MKPointAnnotation()
        destinationAnnotation.title = toname

        if let location = destinationPlacemark.location {
            destinationAnnotation.coordinate = location.coordinate
        }


        // 6.
        self.mapView.showAnnotations([sourceAnnotation,destinationAnnotation], animated: true )

        mapView.frame = CGRect (x: 0, y: 90, width: SW, height: 250)
        view.addSubview(mapView)



        // end map

i want to change this regular shape by UIbutton.

how can i do that ?

i want to change this

let destinationAnnotation = MKPointAnnotation()
    destinationAnnotation.title = toname

    if let location = destinationPlacemark.location {
        destinationAnnotation.coordinate = location.coordinate
    }

to custom UIbutton

like this

 let mapfrom = UIButton()
        mapfrom.frame = CGRect (x: 16, y: 5 , width: SW - 173, height: 45)
        mapfrom.backgroundColor = UIColor.init(red: 250/255, green: 250/255, blue: 250/255, alpha: 1)
        mapfrom.layer.borderColor = UIColor.init(red: 0, green: 151/255, blue: 255/255, alpha: 1).cgColor
        mapfrom.layer.borderWidth = 1;
        mapfrom.layer.cornerRadius = 10.0;
        mapfrom.setTitle(NSLocalizedString("From:", comment: "") + " " + iconetype + " " + fromname,for: .normal)
        mapfrom.setTitleColor(UIColor.init(red: 0, green: 151/255, blue: 255/255, alpha: 1), for: .normal)
        mapfrom.layer.masksToBounds = true
        mapfrom.layer.cornerRadius = 10
        mapfrom.addTarget(self, action: #selector(openmapfrom), for: .touchUpInside)
        view.addSubview(mapfrom)

i hope my question is clear ?

  • 1
    Possible duplicate of [How to create Custom MKAnnotationView and custom annotation title and subtitle](https://stackoverflow.com/questions/16252764/how-to-create-custom-mkannotationview-and-custom-annotation-title-and-subtitle) – Yun CHEN Feb 25 '18 at 11:32

1 Answers1

0

This can be accomplished by adding a UIButton to the rightCalloutAccessoryView of the MKPinAnnotationView. Here is an example:

        func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
            if !(annotation is MKUserLocation) {
                let pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: String(annotation.hash))

                let rightButton = UIButton(type: .contactAdd)
                rightButton.tag = annotation.hash

                pinView.animatesDrop = true
                pinView.canShowCallout = true
                pinView.rightCalloutAccessoryView = rightButton

                return pinView
            }
            else {
                return nil
            }
    }

This is what that'll look like: Click here

Rashed
  • 2,349
  • 11
  • 26