MKMapView:自定义视图代替了注释钉

我要显示图像MKMapView而不是小别针。有人可以在这里放置一些有用的代码,或告诉它怎么做吗?


谢谢!


编辑


-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:

(id <MKAnnotation>)annotation {

MKPinAnnotationView *pinView = nil; 

if(annotation != mapView.userLocation) 

{

    static NSString *defaultPinID = @"com.invasivecode.pin";

    pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];

    if ( pinView == nil ) pinView = [[MKPinAnnotationView alloc]

                                      initWithAnnotation:annotation reuseIdentifier:defaultPinID];


    pinView.pinColor = MKPinAnnotationColorGreen; 

    pinView.canShowCallout = YES;

    pinView.animatesDrop = YES;

    pinView.image = [UIImage imageNamed:@"pinks.jpg"];    //as suggested by Squatch

else {

    [mapView.userLocation setTitle:@"I am here"];

}

return pinView;

}

我希望我的图片pinks.jpg会显示在地图上,固定位置而不是默认的大头针视图(摇滚大头针形)。但是我仍然得到该引脚的默认图像。


大话西游666
浏览 583回答 3
3回答

Smart猫小萌

这是Swift 3的答案。如果可能,它将使注释视图出队,否则将创建新视图:func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {&nbsp; &nbsp; // Don't want to show a custom image if the annotation is the user's location.&nbsp; &nbsp; guard !(annotation is MKUserLocation) else {&nbsp; &nbsp; &nbsp; &nbsp; return nil&nbsp; &nbsp; }&nbsp; &nbsp; // Better to make this class property&nbsp; &nbsp; let annotationIdentifier = "AnnotationIdentifier"&nbsp; &nbsp; var annotationView: MKAnnotationView?&nbsp; &nbsp; if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {&nbsp; &nbsp; &nbsp; &nbsp; annotationView = dequeuedAnnotationView&nbsp; &nbsp; &nbsp; &nbsp; annotationView?.annotation = annotation&nbsp; &nbsp; }&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)&nbsp; &nbsp; &nbsp; &nbsp; annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)&nbsp; &nbsp; }&nbsp; &nbsp; if let annotationView = annotationView {&nbsp; &nbsp; &nbsp; &nbsp; // Configure your annotation view here&nbsp; &nbsp; &nbsp; &nbsp; annotationView.canShowCallout = true&nbsp; &nbsp; &nbsp; &nbsp; annotationView.image = UIImage(named: "yourImage")&nbsp; &nbsp; }&nbsp; &nbsp; return annotationView}Swift 2.2:func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {&nbsp; &nbsp; // Don't want to show a custom image if the annotation is the user's location.&nbsp; &nbsp; guard !annotation.isKindOfClass(MKUserLocation) else {&nbsp; &nbsp; &nbsp; &nbsp; return nil&nbsp; &nbsp; }&nbsp; &nbsp; // Better to make this class property&nbsp; &nbsp; let annotationIdentifier = "AnnotationIdentifier"&nbsp; &nbsp; var annotationView: MKAnnotationView?&nbsp; &nbsp; if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier) {&nbsp; &nbsp; &nbsp; &nbsp; annotationView = dequeuedAnnotationView&nbsp; &nbsp; &nbsp; &nbsp; annotationView?.annotation = annotation&nbsp; &nbsp; }&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)&nbsp; &nbsp; &nbsp; &nbsp; av.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure)&nbsp; &nbsp; &nbsp; &nbsp; annotationView = av&nbsp; &nbsp; }&nbsp; &nbsp; if let annotationView = annotationView {&nbsp; &nbsp; &nbsp; &nbsp; // Configure your annotation view here&nbsp; &nbsp; &nbsp; &nbsp; annotationView.canShowCallout = true&nbsp; &nbsp; &nbsp; &nbsp; annotationView.image = UIImage(named: "yourImage")&nbsp; &nbsp; }&nbsp; &nbsp; return annotationView}

慕姐4208626

我同意安娜的答案,我想展示一下swift3的外观。这个答案与其他许多选项一样,比如调整图像大小,从array和ect中获取图像列表。func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {&nbsp; &nbsp; &nbsp; &nbsp; if let annotation = annotation as? PetrolStation {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let identifier = "pinAnnotation"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var view: MKAnnotationView&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; as? MKPinAnnotationView { // 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dequeuedView.annotation = annotation&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; view = dequeuedView&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; view = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; view.canShowCallout = true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //here We put a coordinates where we like to show bubble with text information up on the pin image&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; view.calloutOffset = CGPoint(x: -7, y: 7)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Here this is a array of images&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let pinImage = PetrolItem[activePlace].imgPetrol?[activePlace]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Here we set the resize of the image&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let size = CGSize(width: 30, height: 30)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UIGraphicsBeginImageContext(size)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pinImage?.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let resizeImage = UIGraphicsGetImageFromCurrentImageContext()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UIGraphicsEndImageContext()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; view.image = resizeImage&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Here we like to put into bubble window a singe for detail Informations&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) as UIView&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Here we make change of standard pin image with our image&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; view.image = resizeImage&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return view&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return nil&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

iOS