上的MKMapView自销的iOS?(Custom pin on MKMapView in iOS?

2019-08-21 20:53发布

我曾尝试只是一切得到显示,而不是在我的MKMapView默认的红针的图像。

有很多答案这个在互联网上,但所有的人都不断给我这个:


- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    if([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    NSString *annotationIdentifier = @"CustomViewAnnotation";
    MKAnnotationView* annotationView = [mapview dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
    if(!annotationView)
    {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                      reuseIdentifier:annotationIdentifier]];
    }
    annotationView.image = [UIImage imageNamed:@"map_location_pin.png"];
    annotationView.canShowCallout= YES;

    return annotationView;
}

这不被它的外观帮助我的人,它只是一个方法。

请告诉我怎么用这使该方法返回一个注释具有自定义图像。 几乎所有其他的答案告诉我,来实现与没有进一步的解释该方法。

当我把它? 我该如何称呼呢?

谢谢!

Answer 1:

设法让这个固定在我自己的,这里是我的4个步骤做:

第1步:你必须设置的MapView到ViewController委托:

MapViewController.h

 @interface MapViewController : UIViewController <MKMapViewDelegate> {
        IBOutlet MKMapView *mapView;
    }
@property (nonatomic, nonatomic) IBOutlet MKMapView *mapView;

MapViewController.m

- (void)viewDidLoad
{    
    [super viewDidLoad];
    [mapView setDelegate:self];
}

步骤2:实施所述方法:

MapViewController.m

-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"current"];
    //MyPin.pinColor = MKPinAnnotationColorPurple;

UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[advertButton addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];

/*MyPin.rightCalloutAccessoryView = advertButton;
 MyPin.draggable = YES;

 MyPin.animatesDrop=TRUE;
 MyPin.canShowCallout = YES;*/
MyPin.highlighted = NO;
MyPin.image = [UIImage imageNamed:@"myCustomPinImage"];

return MyPin;
}

第三步:创建一个名为“注释”(或任何其他名称)班

Annotation.h

#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>

@interface Annotation : NSObject <MKAnnotation> {

    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;

}

@property(nonatomic, assign) CLLocationCoordinate2D coordinate;
@property(nonatomic, copy) NSString *title;
@property(nonatomic, copy) NSString *subtitle;

@end

Annotation.m

#import "Annotation.h"

@implementation Annotation
@synthesize coordinate, title, subtitle;

@end

第4步:添加注释,你将有你的自定义图像!

MapViewController.m

MKCoordinateRegion Bridge = { {0.0, 0.0} , {0.0, 0.0} };
        Bridge.center.latitude = [[[testArr objectAtIndex:updates] objectAtIndex:1] floatValue];
        Bridge.center.longitude = [[[testArr objectAtIndex:updates] objectAtIndex:0] floatValue];
        Bridge.span.longitudeDelta = 0.01f;
        Bridge.span.latitudeDelta = 0.01f;

        Annotation *ann = [[Annotation alloc] init];
        ann.title = @"I'm a pin";
        ann.subtitle = @"Your subtitle";
        ann.coordinate = Bridge.center;
        [mapView addAnnotation:ann];

我希望这有帮助!



文章来源: Custom pin on MKMapView in iOS?