我希望我的问题很清楚,我是 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
}
到自定义 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)
我希望我的问题很清楚?
最佳答案
这可以通过向 MKPinAnnotationView 的 rightCalloutAccessoryView 添加一个 UIButton 来实现。这是一个例子:
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
}
}
这就是它的样子: Click here
关于ios - 苹果 map 工具包 : how to insert UIbutton instate of location pins,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48972853/