Mkmapview MapKit未在iOS9上显示自定义注释pin图像
我的代码从iOS 7到iOS 8运行良好。随着昨天的更新,我的pin上的自定义图像被标准pin图像替换。 有什么建议吗 我的代码:Mkmapview MapKit未在iOS9上显示自定义注释pin图像,mkmapview,mapkit,mkannotation,ios9,mkannotationview,Mkmapview,Mapkit,Mkannotation,Ios9,Mkannotationview,我的代码从iOS 7到iOS 8运行良好。随着昨天的更新,我的pin上的自定义图像被标准pin图像替换。 有什么建议吗 我的代码: extension ViewController: MKMapViewDelegate { func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView! { if annotation is MKUs
extension ViewController: MKMapViewDelegate {
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView! {
if annotation is MKUserLocation {
return nil
}
let reuseId = String(stringInterpolationSegment: annotation.coordinate.longitude)
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.image = getRightImage(annotation.title!!)
}
let button = UIButton(type: UIButtonType.DetailDisclosure)
pinView?.rightCalloutAccessoryView = button
return pinView
}
}
获取图像的函数根据名称返回一个UIImage
:
func getRightImage (shopName:String)-> UIImage{
var correctImage = UIImage()
switch shopName
{
case "Kaisers":
correctImage = UIImage(named: "Kaisers.jpg")!
default:
correctImage = UIImage(named: "sopiconsmall.png")!
}
return correctImage
}
不,地图如下所示:
不要创建一个
MKPinAnnotationView
,而是创建一个普通的MKAnnotationView
MKPinAnnotationView子类倾向于忽略image属性,因为它被设计为仅显示标准的红色、绿色和紫色插针(通过pinColor属性)
当您切换到
MKAnnotationView
时,您还必须注释掉animatesDrop行,因为该属性特定于MKPinAnnotationView
以下代码在所有iOS 6到iOS 9设备上运行良好:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
// create a proper annotation view, be lazy and don't use the reuse identifier
MKAnnotationView *view = [[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@"identifier"];
// create a disclosure button for map kit
UIButton *disclosure = [UIButton buttonWithType:UIButtonTypeContactAdd];
[disclosure addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(disclosureTapped)]];
view.rightCalloutAccessoryView = disclosure;
view.enabled = YES;
view.image = [UIImage imageNamed:@"map_pin"];
return view;
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释
{
//创建一个适当的注释视图,不要使用重用标识符
MKAnnotationView*view=[[MKAnnotationView alloc]initWithAnnotation:annotation
reuseIdentifier:@“标识符”];
//为地图工具包创建一个公开按钮
UIButton*披露=[UIButton按钮类型:UIButtonTypeContactAdd];
[披露添加GestureRecognitor:[[UITapGestureRecognitor alloc]initWithTarget:self
操作:@selector(dispositiontapped)];
view.RightCallout访问视图=公开;
view.enabled=是;
view.image=[UIImage ImageName:@“map_pin”];
返回视图;
}
适用于Swift 4
func mapView(mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let reuseId = String(stringInterpolationSegment: annotation.coordinate.longitude)
var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
if pinView == nil {
pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.image = getRightImage(annotation.title!!)
}
let button = UIButton(type: UIButtonType.DetailDisclosure)
pinView?.rightCalloutAccessoryView = button
return pinView
}
我也有同样的问题。你建议的解决方案帮我解决了,谢谢。谢谢!太好了,工作得很好。谢谢你的解决方案,你救了我一个晚上!我有一个疯狂的问题,自定义pin图像是在我第二次打开地图后才加载的。并更改为
MKAnnotationView
修复了该问题!谢谢