我希望我的问题很清楚,我是Apple Map Kit的初学者
如何插入Uibutton的位置销钉
我有此代码:
巫婆显示出常规的销。
// 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
我想通过uibutton改变这种常规形状。
我该怎么做?
我想更改此
let destinationAnnotation = MKPointAnnotation()
destinationAnnotation.title = toname
if let location = destinationPlacemark.location {
destinationAnnotation.coordinate = location.coordinate
}
to 自定义Uibutton
喜欢这个
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)
我希望我的问题很清楚?
这可以通过将Uibutton添加到MkpinAnnotationView的RightCalloutAccessoryView来完成。这是一个示例:
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
}
}
这就是外观: 单击此处